*Title of the study: Saving for Multiple Financial Needs: Evidence from Lockboxes and Mobile Money in Malawi”
*Authors:  Shilpa Aggarwal, Valentina Brailovskaya, Jonathan Robinson
*Purpose of the Code: Run the analysis 

*# delimit
clear

clear matrix
clear mata
set more off
set maxvar 15000


global version "1"

if "${version}"=="1" {
		global path1	"X"
		global path2    "X"
		
		}





************************************************************************************
**********************************ANALYSIS******************************************
************************************************************************************


*******************************************************
*Randomization check - From Baseline and Intake Survey
*Table 1, A3
*******************************************************

use "${path1}MAP2_Working_File.dta", clear

*generate variables that indicate whether a person was found at least once during follow ups 

gen hfps_dum=(hfps_call_date_raw!=.)
bysort id: egen tot_num_surveys=total(hfps_dum)
	
gen hfps_present=(tot_num_surveys>0)
gen ms_present=(ms_round==1 | ms_round==2)
gen present=(hfps_present==1 | ms_present==1)
bysort id: egen present_atleast1=max(present)

drop hfps_dum tot_num_surveys hfps_present ms_present present
		
*generating other variables for the table
		
gen behavioral_boxes_sample=1 if one_box==1 | multi_box==1 | control==1
gen mobile_money_sample=1 if one_mm==1 | multi_mm==1 | control==1
gen bl_b23_iron_roof=1 if bl_b23==2
replace bl_b23_iron_roof=0 if bl_b23!=2 & bl_b23!=.

for num 1/27 95: cap egen sd_goal_X=rowsd(bl_sav12*pur_X)
egen bl_separates_goals=rowmax(sd_goal_*)
replace bl_separates_goals=1 if bl_separates_goals>0 & bl_separates_goals!=. & bl_total_savplaces>1 & bl_total_savplaces!=.
replace bl_separates_goals=0 if bl_total_savplaces==1
drop bl_sav12_*_pur_o

foreach X in box home rosca vsla bank{
 egen xx=rowtotal(bl_sav12*`X'*pur*)
 gen `X'_multiple_goals=1 if xx>1 & xx!=.
 replace `X'_multiple_goals=0 if xx==1 | xx==0
 drop xx
}
	 
egen bl_1source_multiple_goals=rowtotal(*_multiple_goals)
replace bl_1source_multiple_goals=1 if bl_1source_multiple_goals>0 & bl_1source_multiple_goals!=.


for num 1/27 95: cap egen has_goal_X=rowmax(bl_sav12*pur_X)
egen bl_total_num_goals=rsum(has_goal_*)

gen bl_vsla_rosca=1 if bl_sav10_vsla_dum ==1 | bl_sav10_rosca_dum==1
replace bl_vsla_rosca=0 if bl_sav10_vsla_dum ==0 & bl_sav10_rosca_dum==0

gen bl_has_mobile_money=1 if bl_mm4_use==1
replace bl_has_mobile_money=0 if bl_mm1_heard==0
replace bl_has_mobile_money=0 if bl_mm2_==0
replace bl_has_mobile_money=0 if bl_mm4_use==2 | bl_mm4_use==3

egen bl_uses_mm_transfers=rowtotal(bl_mm15_pur_send_dum bl_mm15_pur_receive_dum), missing
egen bl_uses_mm_savings=rowtotal(bl_mm15_pur_keep_dum bl_mm15_pur_store_dum bl_mm15_pur_save_gen_dum bl_mm15_pur_save_goal_dum), missing

for var bl_uses_mm_transfers bl_uses_mm_savings: replace X=1 if X>0 & X!=. \ replace X=0 if bl_has_mobile_money==0

egen bl_val_inv_equip_w1=rowtotal(bl_c17_w1 bl_c25_w1), missing /*unconditional on having inventory*/
replace bl_sav_mm30_dum=0 if bl_sav_mm30_dum==. /*making unconditional on having MM*/

*winsorize variables for intake survey analysis

local win w1
local 100minuswin=99

egen pre_exp_tot=rowtotal(pre_exp4_today pre_exp3_today pre_exp2_today pre_exp1_today), missing
cap gen pre_tr_non_sp_net= pre_tr_give_non_sp_total - pre_tr_receive_non_sp_total/*given -received all other*/
cap gen pre_tr_sp_net= pre_tr_give_sp_am - pre_tr_receive_sp_am /*given -received for  spouse*/

foreach var in ///
 b8_profit_today c5_farm_hrs d_dep_tot_today d_wit_tot_today exp1_today exp_tot tr_non_sp_net tr_sp_net{			
	gen pre_`var'_`win'=pre_`var'
	qui sum pre_`var' if pre_`var'!=0, d
	replace pre_`var'_`win'=r(p`100minuswin') if pre_`var'>r(p`100minuswin') & !missing(pre_`var')
	}




*****************************************************************
*Table1 - Randomization regressions From the Baseline 
*****************************************************************

* Running Regression

*#delimit
local varlist ///
 bl_a2_gender bl_b20_farm bl_years_educ bl_total_land_owned bl_b23_b24_assets_animal_val_w1 bl_b23_iron_roof bl_b23_phonemobile_dum  ///
 bl_has_mobile_money bl_q35_mm_dist_owner_min bl_businessclass_1 bl_c6_rev_ave_w1 bl_c6_prof_ave_w1 bl_c17_w1 bl_total_savings_w1 ///
 bl_sav_mm30_dum bl_sav10_bank_dum bl_vsla_rosca bl_sav10_home_dum bl_sav10_box_dum 

tokenize `varlist'
local sumvars=wordcount("`varlist'")
di `sumvars'


sort id
gen var=""
for any mean sd N: gen X=.
local i 1
while `i' <= `sumvars' {

		local append_replace="replace"
		if `i'!=1 { 
		local append_replace="append"
		}
		
		*1a. Full sample
		qui reg ``i'' one_box multi_box one_mm multi_mm [pweight=weight_inv] if (behavioral_boxes_sample==1 | mobile_money_sample==1) & id!=id[_n-1] & present_atleast1==1
		qui test one_box multi_box one_mm multi_mm
		local p_joint=r(p)
		
		qui test one_box multi_box
		local p_joint_box=r(p)
		
		qui test one_mm multi_mm
		local p_joint_mm=r(p)
		
		qui sum ``i'' if control==1 & e(sample)
		local mean=r(mean)
		local sd=r(sd)
		outreg2 using "${path2}\table_1.out", `append_replace' nonote se symb(***, **, *) dec(2) nocons ///
		 addstat("control mean", `mean', "control sd", `sd', ///
			 "F stat one box=multi box=one mm= multi mm - pvalue",`p_joint', ///
			 "F stat box one = multi box= 0 - pvalue",`p_joint_box', ///
			 "F stat one mm = multi mm=0- pvalue",`p_joint_mm' )
			 
		local i = `i' + 1
		}

	
		
		
	
************************************************************************
*Table A3 - Randomization regressions From the intake survey 
************************************************************************

*Running Regression

*#delimit
set more off
local varlist pre_b1_work_hrs pre_b8_profit_today_w1 pre_c5_farm_hrs_w1 pre_d_dep_tot_today_w1 pre_d_wit_tot_today_w1 ///
 pre_exp1_today_w1 pre_exp_tot_w1 pre_tr_non_sp_net_w1 pre_tr_sp_net_w1
 

 *converting weekly values to daily
 foreach var of varlist pre_c5_farm_hrs_w1 pre_tr_non_sp_net_w1 pre_tr_sp_net_w1{
 replace `var'=`var'/7
 }
 
	tokenize `varlist'
	local sumvars=wordcount("`varlist'")
	di `sumvars'

		sort id
		cap gen var=""
		for any mean sd N: cap gen X=.
		local i 1
		
		while `i' <= `sumvars' {

			local append_replace="replace"
			if `i'!=1 { 
			local append_replace="append" 
			}
		
		*1. Behavioral boxes
		*1a. Full sample
			qui reg ``i'' one_box multi_box one_mm multi_mm [pweight=weight_inv] if (behavioral_boxes_sample==1 | mobile_money_sample==1) & id!=id[_n-1] & present_atleast1==1
			qui test one_box multi_box one_mm multi_mm
		local p_joint=r(p)
		
		qui test one_box multi_box
		local p_joint_box=r(p)
		
		qui test one_mm multi_mm
		local p_joint_mm=r(p)
		
		qui sum ``i'' if control==1 & e(sample)
		local mean=r(mean)
		local sd=r(sd)
			qui sum ``i'' if control==1 & e(sample)
			local mean=r(mean)
			local sd=r(sd)
			outreg2 using "${path2}\table_a3_full.out", `append_replace' nonote se symb(***, **, *) dec(2) nocons ///
			 addstat("control mean", `mean', "control sd", `sd', ///
			 "F stat one box=multi box=one mm= multi mm - pvalue",`p_joint', ///
			 "F stat box one = multi mm= 0 - pvalue",`p_joint_box', ///
			 "F stat one mm = multi mm=0- pvalue",`p_joint_mm' )

			 
			*1b. HFPS sample
			qui reg ``i'' one_box multi_box one_mm multi_mm [pweight=weight_inv] if (behavioral_boxes_sample==1 | mobile_money_sample==1)  & id!=id[_n-1] & hfps==1 & present_atleast1==1
			qui test one_box multi_box one_mm multi_mm
		local p_joint=r(p)
		
		test one_box multi_box
		local p_joint_box=r(p)
		
		
		qui test one_mm multi_mm
		local p_joint_mm=r(p)
		
		qui sum ``i'' if control==1 & e(sample)
		local mean=r(mean)
		local sd=r(sd)
			qui sum ``i'' if control==1 & e(sample)
			local mean=r(mean)
			local sd=r(sd)
			
			outreg2 using "${path2}\table_a3_hfps.out", `append_replace' nonote se symb(***, **, *) dec(2) nocons ///
			 addstat("control mean", `mean', "control sd", `sd', ///
			 "F stat one box=multi box=one mm= multi mm - pvalue",`p_joint', ///
			 "F stat box one = multi mm= 0 - pvalue",`p_joint_box', ///
			 "F stat one mm = multi mm=0- pvalue",`p_joint_mm' )
			 
			local i = `i' + 1
			local append_replace="append" 					
			
			}
			
					
	
************************************************************************************************
* Take-up sum stats; Take-up of project accounts, survey responses; and Long-Term Usage
************************************************************************************************

use "${path1}MAP2_Working_File.dta", clear

sort id
local win w5
local 100minuswin=95

cap gen bl_has_mobile_money=1 if bl_mm4_use==1
replace bl_has_mobile_money=0 if bl_mm1_heard==0
replace bl_has_mobile_money=0 if bl_mm2_==0
replace bl_has_mobile_money=0 if bl_mm4_use==2 | bl_mm4_use==3


*1 - Lockbox check

	gen lb_took_box=(pre_num_boxes_taken>0) if pre_num_boxes_taken!=. & (multi_box==1 | one_box==1)
	gen lb_took_multiple_boxes=(pre_num_boxes_taken>1) if pre_num_boxes_taken!=. & multi_box==1
	gen lb_took_3_boxes=(pre_num_boxes_taken==3) if pre_num_boxes_taken!=. & multi_box==1
	gen lb_non0_bal=(lb_balance_box_dec17_USD!=0) if lb_balance_box_dec17_USD!=.
	replace lb_non0_bal=. if lb_balance_box_dec17_USD>120000

	gen lb_made_1dep=(lb_total_num_deposits>=1) if lb_total_num_deposits!=.
	replace lb_made_1dep=. if lb_total_num_deposits>100000
	gen lb_made_2dep=(lb_total_num_deposits>=2) if lb_total_num_deposits!=.
	replace lb_made_2dep=. if lb_total_num_deposits>100000
	gen lb_made_5dep=(lb_total_num_deposits>=5) if lb_total_num_deposits!=.
	replace lb_made_5dep=. if lb_total_num_deposits>100000

	*Converting to monthly values - 16Feb2020
		
		foreach var of varlist lb_total_amt_dep_USD lb_total_num_deposits lb_total_amt_wd_USD lb_total_num_wd{
		gen `var'_m=`var'/5 /*approximately 5 months between the july & december*/
		}


	*Winsorizing
	foreach X in lb_balance_box_dec17_USD lb_total_amt_dep_USD_m lb_total_num_deposits_m ///
		lb_total_amt_wd_USD_m lb_total_num_wd_m ///
		lb_total_amt_dep_USD lb_total_num_deposits ///
		lb_total_amt_wd_USD lb_total_num_wd {
	 sum `X' if `X'!=0, d 
	 gen `X'_`win'=`X' 
	 replace `X'_`win'=r(p`100minuswin') if `X'>r(p`100minuswin') & `X'!=.
	 }
 
*2 - Airtel administrative data

	gen at_opened_acct=(pre_num_mm_acc_final>0) if pre_num_mm_acc_final!=. & (multi_mm==1 | one_mm==1)
	gen at_took_multiple_acct=(pre_num_mm_acc_final>1) if pre_num_mm_acc_final!=. & multi_mm==1

	gen at_made_2dep=(at_total_dep_num>=2)
	replace at_made_2dep=. if missing(at_total_dep_num)

	gen at_made_5dep=(at_total_dep_num>=5)
	replace at_made_5dep=. if missing(at_total_dep_num)
	
	gen at_non0_bal=( at_total_balance_USD!=0) if at_total_balance_USD!=.
	replace at_non0_bal=. if at_total_balance_USD>1000000

	foreach period in may18 dec17 aug19{

		gen at_made_1dep_`period'=(at_total_dep_num_`period'>=1)
		replace at_made_1dep_`period'=. if missing(at_total_dep_num_`period')

		gen at_made_2dep_`period'=(at_total_dep_num_`period'>=2)
		replace at_made_2dep_`period'=. if missing(at_total_dep_num_`period')

		gen at_made_5dep_`period'=(at_total_dep_num_`period'>=5)
		replace at_made_5dep_`period'=. if missing(at_total_dep_num_`period')

		gen at_non0_bal_`period'=(at_total_balance_`period'_USD!=0)
		replace at_non0_bal_`period'=. if missing(at_total_dep_num_`period')
	 }
 
		 
		 *generating May2018-August 2019 Variable
		 
		 egen at_cashin_amt_jun18_aug19=rowtotal(at_cashin_amt_6_2018 at_cashin_amt_7_2018 at_cashin_amt_8_2018 ///
		 at_cashin_amt_9_2018 at_cashin_amt_10_2018 at_cashin_amt_1_2019 at_cashin_amt_2_2019 at_cashin_amt_3_2019 ///
		 at_cashin_amt_4_2019 at_cashin_amt_5_2019 at_cashin_amt_6_2019 at_cashin_amt_7_2019 at_cashin_amt_8_2019), missing
		 
		  egen at_cashout_amt_jun18_aug19=rowtotal(at_cashout_amt_6_2018 at_cashout_amt_7_2018 at_cashout_amt_8_2018 at_cashout_amt_9_2018 /// 
		  at_cashout_amt_10_2018 at_cashout_amt_1_2019 at_cashout_amt_2_2019 at_cashout_amt_3_2019 at_cashout_amt_4_2019 at_cashout_amt_5_2019 /// 
		  at_cashout_amt_6_2019 at_cashout_amt_7_2019 at_cashout_amt_8_2019), missing
		  
		  gen at_cashin_amt_jun18_aug19_dum=at_cashin_amt_jun18_aug19
		  replace at_cashin_amt_jun18_aug19_dum=1 if at_cashin_amt_jun18_aug19_dum>0 & !missing(at_cashin_amt_jun18_aug19_dum)
		  
		 gen at_cashout_amt_jun18_aug19_dum=at_cashout_amt_jun18_aug19
		  replace at_cashout_amt_jun18_aug19_dum=1 if at_cashout_amt_jun18_aug19_dum>0 & !missing(at_cashout_amt_jun18_aug19_dum)
		  
		  gen at_run_balance_num_j18a19_dum=at_ave_run_balance_num_j18a19
		  replace at_run_balance_num_j18a19_dum=1 if at_run_balance_num_j18a19_dum>0 & !missing(at_run_balance_num_j18a19_dum)
		  
	  
	  *converting to monthly averages
	  
	  foreach var of varlist 	///
			at_total_balance_dec17_USD  	at_total_balance_may18_USD	 		at_total_balance_aug19_USD ///
			at_total_dep_amt_dec17_USD 		at_total_dep_amt_may18_USD		at_total_dep_amt_aug19_USD ///
			at_total_wd_amt_dec17_USD 		at_total_wd_amt_may18_USD 		at_total_wd_amt_aug19_USD ///
			at_total_wd_num_dec17			at_total_wd_num_may18 			at_total_wd_num_aug19 ///
			at_total_dep_num_dec17 			at_total_dep_num_may18			at_total_dep_num_aug19{
			
			gen `var'_m=`var'/10/*jul - april*/
	  
					
	  }
	
	*winsorizing
	foreach X in at_total_balance_USD ///
		at_total_dep_num ///
		at_total_wd_num ///
		at_total_dep_amt_USD ///
		at_total_wd_amt_USD ///
		at_total_balance_dec17_USD  	at_total_balance_may18_USD	 		at_total_balance_aug19_USD ///
		at_total_dep_amt_dec17_USD_m 		at_total_dep_amt_may18_USD_m		at_total_dep_amt_aug19_USD_m ///
		at_total_wd_amt_dec17_USD_m 		at_total_wd_amt_may18_USD_m 		at_total_wd_amt_aug19_USD_m ///
		at_total_wd_num_dec17_m			at_total_wd_num_may18_m 			at_total_wd_num_aug19_m ///
		at_total_dep_num_dec17_m 			at_total_dep_num_may18_m			at_total_dep_num_aug19_m ///
		at_ave_run_balance_num_may18 ///	
		at_cashin_amt_jun18_aug19 ///
		at_cashout_amt_jun18_aug19 ///
		at_ave_run_balance_num_j18a19 ///	
		at_total_dep_amt_may18_USD  at_total_dep_amt_aug19_USD {
		 sum `X' if id!=id[_n-1] & `X'!=0, d
		 gen `X'_`win'=`X' 
		 replace `X'_`win'=r(p`100minuswin') if `X'>r(p`100minuswin') & `X'!=.
	 }


*3 - HFPS data	
	*Usage measured in HFPS
	egen hfps_d_dep_box_IPA_tot_week=rowtotal(hfps_d_dep_box_silver_week hfps_d_dep_box_black_week hfps_d_dep_box_brown_week), missing
	egen hfps_d_wit_box_IPA_tot_week=rowtotal(hfps_d_wit_box_silver_week hfps_d_wit_box_black_week hfps_d_wit_box_brown_week), missing
	egen hfps_d_dep_mm_IPA_tot_week=rowtotal(hfps_d_dep_mm1_week hfps_d_dep_mm2_week), missing
	egen hfps_d_wit_mm_IPA_tot_week=rowtotal(hfps_d_wit_mm1_week hfps_d_wit_mm2_week), missing

	*Winsorizing
	foreach X in hfps_d_dep_box_IPA_tot_week hfps_d_wit_box_IPA_tot_week hfps_d_dep_mm_IPA_tot_week hfps_d_wit_mm_IPA_tot_week {
	 sum `X' if `X'!=0, d 
	 gen `X'_`win'=`X' 
	 replace `X'_`win'=r(p`100minuswin') if `X'>r(p`100minuswin') & `X'!=.
	 }

	egen hfps_AVE_dep_box_`win'=mean(hfps_d_dep_box_IPA_tot_week_`win') if hfps==1, by(id)
	egen hfps_TOT_dep_box_`win'=total(hfps_d_dep_box_IPA_tot_week_`win') if hfps==1, by(id)
	egen hfps_TOT_dep_box=total(hfps_d_dep_box_IPA_tot_week) if hfps==1, by(id)
		gen hfps_TOT_dep_box_m_`win'=hfps_TOT_dep_box_`win'/3.5 /*monthly*/
	egen hfps_TOT_wd_box_`win'=total(hfps_d_wit_box_IPA_tot_week_`win') if hfps==1, by(id)
		gen hfps_TOT_wd_box_m_`win'=hfps_TOT_wd_box/3.5 /*monthly*/
	gen hfps_atleast1_dep_box=1 if hfps_AVE_dep_box_`win'>0 & hfps_AVE_dep_box_`win'!=.
	replace hfps_atleast1_dep_box=0 if hfps_TOT_dep_box_`win'==0
	gen log_hfps_AVE_dep_box_`win'=asinh(hfps_AVE_dep_box_`win')
	gen log_hfps_TOT_dep_box_`win'=asinh(hfps_TOT_dep_box_`win')
		

	egen hfps_AVE_dep_mm_`win'=mean(hfps_d_dep_mm_IPA_tot_week_`win') if hfps==1, by(id)
	egen hfps_TOT_dep_mm_`win'=total(hfps_d_dep_mm_IPA_tot_week_`win') if hfps==1, by(id)
		gen hfps_TOT_dep_mm_m_`win'=hfps_TOT_dep_mm_`win'/3.5 /*monthly*/
		
	egen hfps_TOT_wd_mm_`win'=total(hfps_d_wit_mm_IPA_tot_week_`win') if hfps==1, by(id)
		gen hfps_TOT_wd_mm_m_`win'=hfps_TOT_wd_mm_`win'/3.5 /*monthly*/
		
	gen hfps_atleast1_dep_mm=1 if hfps_AVE_dep_mm_`win'>0 & hfps_AVE_dep_mm_`win'!=.
	replace hfps_atleast1_dep_mm=0 if hfps_TOT_dep_mm_`win'==0
	gen log_hfps_AVE_dep_mm_`win'=asinh(hfps_AVE_dep_mm_`win')
	gen log_hfps_TOT_dep_mm_`win'=asinh(hfps_TOT_dep_mm_`win')

	egen hfps_TOT_dep_mm=total(hfps_d_dep_mm_IPA_tot_week) if hfps==1, by(id)
	egen hfps_TOT_wd_mm=total(hfps_d_wit_mm_IPA_tot_week) if hfps==1, by(id)
	
	*verified balance in boxes
	egen lb_am_in_box_all=rowtotal(lb_am_in_box_silver lb_am_in_box_black lb_am_in_box_brown)
	replace lb_am_in_box_all=. if lb_am_in_box_silver==. & lb_am_in_box_black==. & lb_am_in_box_brown==.
	lab var lb_am_in_box_all "Verified balance in all boxes"

*4 - Monitoring surveys

	*Winsorizing
	for any ms_d_dep_box_ipa_2m_total ms_d_wit_box_ipa_2m_total ///
	 ms_d_dep_mm_ipa_2m_total ms_d_wit_mm_ipa_2m_total at_mm_running_balance_ms_date lb_am_in_box_all: ///
	 sum X if id!=id[_n-1] & X!=0, d \ ///
	 gen X_`win'=X \ ///
	 replace X_`win'=r(p`100minuswin') if X>r(p`100minuswin') & !missing(X)

	*Sum up total savings in the MS and calculate averages
	egen ms_AVE_dep_box_`win'=mean(ms_d_dep_box_ipa_2m_total_`win'), by(id)
	egen ms_TOT_dep_box_`win'=sum(ms_d_dep_box_ipa_2m_total_`win'), by(id)
		gen ms_TOT_dep_box_m_`win'=ms_TOT_dep_box_`win'/2 /*1 measure spans 2 months*/
	egen ms_TOT_wd_box_`win'=sum(ms_d_wit_box_ipa_2m_total_`win'), by(id)
		gen ms_TOT_wd_box_m_`win'=ms_TOT_wd_box_`win'/2/*1 measure spans 2 months*/


	egen ms_AVE_dep_box=mean(ms_d_dep_box_ipa_2m_total), by(id)
	egen ms_TOT_dep_box=sum(ms_d_dep_box_ipa_2m_total), by(id)
	egen ms_TOT_wd_box=sum(ms_d_wit_box_ipa_2m_total), by(id)

	gen ms_atleast1_dep_box=1 if ms_TOT_dep_box_`win'!=0 & ms_TOT_dep_box_`win'!=.
	replace ms_atleast1_dep_box=0 if ms_TOT_dep_box_`win'==0

	egen ms_AVE_dep_mm_`win'=mean(ms_d_dep_mm_ipa_2m_total_`win'), by(id)
	egen ms_TOT_dep_mm_`win'=sum(ms_d_dep_mm_ipa_2m_total_`win'), by(id)
	egen ms_TOT_dep_mm=sum(ms_d_dep_mm_ipa_2m_total), by(id)
	egen ms_TOT_wd_mm_`win'=sum(ms_d_wit_mm_ipa_2m_total_`win'), by(id) 
	gen ms_atleast1_dep_mm=1 if ms_TOT_dep_mm_`win'!=0 & ms_TOT_dep_mm_`win'!=.
	replace ms_atleast1_dep_mm=0 if ms_TOT_dep_mm_`win'==0

	
*5 - Generating variables to determine long-term Usage for Box and Mobile Money (Combined)
	
	*generating combined deposits, withdrawals, balance	for August2019
	rename el2*bx* el2*box*
	foreach box in box mm{
	foreach dep in dep wit bal{
	cap gen el2_`dep'_brown_`box'=0
	egen el2_`dep'_all_`box'_aug19=rowtotal(el2_`dep'_silver_`box' el2_`dep'_black_`box' el2_`dep'_brown_`box') if el2_consent_yes==1, missing
	gen el2_`dep'_all_`box'_aug19_dum=(el2_`dep'_all_`box'_aug19>0) if !missing(el2_`dep'_all_`box'_aug19) & el2_consent_yes==1
	}
	gen el2_uses_atleast1`box'=(el2_dep_all_`box'_aug19_dum==1 | el2_wit_all_`box'_aug19_dum==1 | el2_bal_all_`box'_aug19_dum==1) if !missing(el2_dep_all_`box'_aug19_dum) & el2_consent_yes==1
	}

	*gerating usage dummies for August2019
	foreach var in at_cashin_amt_8_2019 at_cashout_amt_8_2019 at_ave_run_balance_num_aug19{
	gen `var'_dum=(`var'>0)
	replace `var'_dum=. if missing(`var')
	}

	*has at least 1 box
	gen el2_has_atleast1box=(el2_use_silver_box==1 | el2_use_black_box==1 | el2_use_brown_box==1) if el2_use_silver_box!=.
	
	*has at least 1 mm account
	gen el2_has_atleast1mm=(el2_use_silver_mm==1 | el2_use_black_mm==1) if el2_use_silver_mm!=.
	
	
	*Winsorizing
	foreach X in el2_bal_all_mm_aug19 el2_wit_all_mm_aug19 el2_dep_all_mm_aug19 el2_bal_all_box_aug19 el2_wit_all_box_aug19 ///
	el2_dep_all_box_aug19 at_cashin_amt_8_2019 at_cashout_amt_8_2019 at_ave_run_balance_num_aug19 ///
	/*at_cashin_amt_jun18_aug19 at_cashout_amt_jun18_aug19 at_ave_run_balance_num_j18a19*/ {
	sum `X' if id!=id[_n-1] & `X'!=0, d 
	 gen `X'_`win'=`X' 
	 replace `X'_`win'=r(p`100minuswin') if `X'>r(p`100minuswin') & `X'!=. 
	} 
	 
	 *create monthly averages
	 
	 gen at_cashin_amt_jun18_aug19_m_`win'=at_cashin_amt_jun18_aug19_`win'/13 /*13 months*/
	 gen at_cashout_amt_jun18_aug19_m_`win'=at_cashout_amt_jun18_aug19_`win'/13 /*13 months*/

	 
*6 - Generating variables related to determinants of account usage
		 
	gen bl_age_10=bl_age/10
	gen bl_animal_durable_assets=bl_b23_assets_val+ bl_b24_animal_val
	gen log_bl_animal_durable_assets=log(bl_b23_assets_val+ bl_b24_animal_val)
	gen log_total_bl_savings=asinh(total_bl_savings)

	* winsorizing variables for analysis

	gen log_lb_total_amt_dep_USD_`win'=asinh(lb_total_amt_dep_USD_`win')
	 
	sum total_bl_savings if total_bl_savings!=0 & id!=id[_n-1], d
	gen total_bl_savings_w5=total_bl_savings
	replace total_bl_savings_w5=r(p95) if total_bl_savings>r(p95) & total_bl_savings!=.

	sum bl_animal_durable_assets if bl_animal_durable_assets!=0 & id!=id[_n-1], d
	gen bl_animal_durable_assets_w5=bl_animal_durable_assets
	replace bl_animal_durable_assets_w5=r(p95) if bl_animal_durable_assets>r(p95) & bl_animal_durable_assets!=.
	 
	*addressing the 1 missing value in durable assets 
	gen bl_animal_durable_assets_w5_mdum=(bl_animal_durable_assets_w5==.)
	replace bl_animal_durable_assets_w5=0 if bl_animal_durable_assets_w5==.
	  
	for var total_bl_savings_w5 : replace X=X/100
	for var bl_animal_durable_assets_w5: replace X=X/1000

	gen log_at_tot_dep_amt_USD_`win'=asinh(at_total_dep_amt_USD_`win')
	
	
********************************************************************
* Table 2 and A4 - Take-up of project accounts, and survey responses
*********************************************************************
	
*Make sum stats table
	gen description=""
	foreach X in mean p50 sd N diff p {
	gen `X'_overall=. 
	gen `X'_1box=.  
	gen `X'_multibox=. 
	}

*A. Box
		local i 1
		foreach takeup_measure of varlist ///
		 lb_took_box lb_took_multiple_boxes lb_took_3_boxes /*lb_non0_bal*/ ///
		 lb_made_1dep lb_made_2dep lb_made_5dep lb_total_amt_dep_USD_m_`win' ///
		 lb_total_num_deposits_m_`win' lb_total_amt_wd_USD_m_`win' lb_total_num_wd_m_`win' lb_am_in_box_all_`win' ///
		 hfps_atleast1_dep_box hfps_TOT_dep_box_m_`win' hfps_TOT_wd_box_m_`win' ///
		 ms_atleast1_dep_box ms_TOT_dep_box_m_`win' ms_TOT_wd_box_m_`win' {
		 
	 
		
		quietly sum `takeup_measure' if id!=id[_n-1] & (one_box==1 | multi_box==1), d
		quietly replace mean_overall=r(mean) if _n==`i'
		quietly replace description="`takeup_measure'" if _n==`i'
		quietly replace sd_overall=r(sd) if _n==`i'
		quietly replace p50_overall=r(p50) if _n==`i'
		quietly replace N_overall=r(N) if _n==`i'
		
		quietly sum `takeup_measure' if id!=id[_n-1] & (one_box==1), d
		quietly replace mean_1box=r(mean) if _n==`i'	
		quietly replace sd_1box=r(sd) if _n==`i'
		quietly replace p50_1box=r(p50) if _n==`i'
		quietly replace N_1box=r(N) if _n==`i'

		quietly sum `takeup_measure' if id!=id[_n-1] & (multi_box==1), d
		quietly replace mean_multibox=r(mean) if _n==`i'
		quietly replace sd_multibox=r(sd) if _n==`i'
		quietly replace p50_multibox=r(p50) if _n==`i'
		quietly replace N_multibox=r(N) if _n==`i'
		
		preserve
		replace `takeup_measure'=0 if `takeup_measure'==. & one_box==1
		quietly ttest `takeup_measure' if id!=id[_n-1] & (one_box==1 | multi_box==1), by(multi_box)
		restore
		quietly replace p_overall=r(p) if _n==`i'
		quietly replace diff_overall=mean_multibox-mean_1box if _n==`i'

		local i = `i' + 1
		}
	local N=`i'-1
		
	outsheet description mean_1box sd_1box p50_1box N_1box mean_multibox sd_multibox p50_multibox N_multibox ///
	 using "${path2}\table_2_a4_box.out" if _n<=`N', replace

	drop description mean_overall sd_overall p50_overall N_overall mean_1box sd_1box p50_1box N_1box mean_multibox sd_multibox p50_multibox N_multibox diff_1box diff_multibox diff_overall p_overall

	

*B. Mobile money for combined tables
	gen description="" 
	for any mean p50 sd N diff p: gen X_overall=. \ gen X_1mm=. \ gen X_multimm=.

	local i 1
	foreach takeup_measure of varlist /// 
	 at_opened_acct /// 											
	 at_made_1dep_may18 							at_made_2dep_may18 	 /// 
	 at_total_dep_amt_may18_USD_m_`win' 			at_total_dep_num_may18_m_`win' /// 
	 at_total_wd_amt_may18_USD_m_`win' 				at_total_wd_num_may18_m_`win' 	at_ave_run_balance_num_may18_`win' ///
	 hfps_atleast1_dep_mm							hfps_TOT_dep_mm_`win'			hfps_TOT_wd_mm_`win'	///		 
	 ms_atleast1_dep_mm								ms_TOT_dep_mm_`win' 			ms_TOT_wd_mm_`win' ///
	 {
	 
		quietly sum `takeup_measure' if id!=id[_n-1] & (one_mm==1 | multi_mm==1), d
		quietly replace description="`takeup_measure'" if _n==`i'
		quietly replace mean_overall=r(mean) if _n==`i'
		quietly replace sd_overall=r(sd) if _n==`i'
		quietly replace p50_overall=r(p50) if _n==`i'
		quietly replace N_overall=r(N) if _n==`i'
		
		quietly sum `takeup_measure' if id!=id[_n-1] & (one_mm==1), d
		quietly replace mean_1mm=r(mean) if _n==`i'	
		quietly replace sd_1mm=r(sd) if _n==`i'
		quietly replace p50_1mm=r(p50) if _n==`i'
		quietly replace N_1mm=r(N) if _n==`i'
		
		quietly sum `takeup_measure' if id!=id[_n-1] & (multi_mm==1), d
		quietly replace mean_multimm=r(mean) if _n==`i'
		quietly replace sd_multimm=r(sd) if _n==`i'
		quietly replace p50_multimm=r(p50) if _n==`i'
		quietly replace N_multimm=r(N) if _n==`i'
		
		preserve
		replace `takeup_measure'=0 if `takeup_measure'==. & one_mm==1
		quietly ttest `takeup_measure' if id!=id[_n-1] & (one_mm==1 | multi_mm==1), by(multi_mm)
		restore
		quietly replace p_overall=r(p) if _n==`i'
		quietly replace diff_overall=mean_multimm-mean_1mm if _n==`i'
		
		local i = `i' + 1
		}
	local N=`i'-1
		
	outsheet description mean_1mm sd_1mm p50_1mm N_1mm mean_multimm sd_multimm p50_multimm N_multimm using "${path2}\table_2_a4_mm.out" if _n<=`N', replace

	drop description mean_overall sd_overall p50_overall N_overall diff_overall mean_1mm sd_1mm p50_1mm N_1mm diff_1mm mean_multimm sd_multimm p50_multimm N_multimm diff_multimm

	

********************************************************
*Table A14 - Long term usage of accounts
********************************************************

	
*Running Regressions


	foreach box in  box mm{
	
	*box usage vars
	local varlist_box	el2_has_atleast1box		el2_uses_atleast1`box' ///
	 el2_dep_all_`box'_aug19_dum 	el2_dep_all_`box'_aug19_`win'	el2_wit_all_`box'_aug19_dum ///	
	 el2_wit_all_`box'_aug19_`win'	el2_bal_all_`box'_aug19_dum		el2_bal_all_`box'_aug19_`win'
	 
	 *mm usage vars
	 local varlist_mm el2_has_atleast1mm	el2_uses_atleast1`box' ///
	 el2_dep_all_`box'_aug19_dum 	el2_dep_all_`box'_aug19_`win'	el2_wit_all_`box'_aug19_dum 	el2_wit_all_`box'_aug19_`win' /// 	
	 el2_bal_all_`box'_aug19_dum	el2_bal_all_`box'_aug19_`win'				 /// 
	 at_cashin_amt_jun18_aug19_dum 		at_cashin_amt_jun18_aug19_m_`win' ///
	at_ave_run_balance_num_j18a19_`win'
	 
	 
	 
	
	gen description=""
	foreach X in mean p50 sd N diff p{

	gen `X'_overall=. 
	gen `X'_1`box'=. 
	gen `X'_multi`box'=. 
	}

	local i 1

	foreach takeup_measure of local varlist_`box'{


		quietly sum `takeup_measure' if id!=id[_n-1] & (one_`box'==1 | multi_`box'==1), d
		quietly replace mean_overall=r(mean) if _n==`i'
		quietly replace description="`takeup_measure'" if _n==`i'
		quietly replace sd_overall=r(sd) if _n==`i'
		quietly replace p50_overall=r(p50) if _n==`i'
		quietly replace N_overall=r(N) if _n==`i'
		
		quietly sum `takeup_measure' if id!=id[_n-1] & (one_`box'==1), d
		quietly replace mean_1`box'=r(mean) if _n==`i'	
		quietly replace sd_1`box'=r(sd) if _n==`i'
		quietly replace p50_1`box'=r(p50) if _n==`i'
		quietly replace N_1`box'=r(N) if _n==`i'

		quietly sum `takeup_measure' if id!=id[_n-1] & (multi_`box'==1), d
		quietly replace mean_multi`box'=r(mean) if _n==`i'
		quietly replace sd_multi`box'=r(sd) if _n==`i'
		quietly replace p50_multi`box'=r(p50) if _n==`i'
		quietly replace N_multi`box'=r(N) if _n==`i'
		
		preserve
		replace `takeup_measure'=0 if `takeup_measure'==. & one_`box'==1
		quietly ttest `takeup_measure' if id!=id[_n-1] & (one_`box'==1 | multi_`box'==1), by(multi_`box')
		restore
		quietly replace p_overall=r(p) if _n==`i'
		quietly replace diff_overall=mean_multi`box'-mean_1`box' if _n==`i'
		
		local i = `i' + 1
		}
	local N=`i'-1
		
	outsheet description mean_1`box' sd_1`box' p50_1`box' N_1`box' mean_multi`box' sd_multi`box' p50_multi`box' N_multi`box' ///
	 using "${path2}\table_A14_`box'.out" if _n<=`N', replace

	drop description *overall
	}



	
************************************************************
*Table 3. Determinants of Account Usage (Take-up regressions)
*************************************************************


sort id

local regressors ///
 bl_agent_dist_hrs  bl_female bl_married bl_fem_married bl_age_10 bl_years_educ bl_kids_u18 ///  
 bl_sav10_box_dum bl_sav10_bank_dum bl_has_mobile_money ///
 log_total_bl_savings log_bl_animal_durable_assets bl_taxed


*Box regressions

foreach takeup_measure of varlist lb_total_amt_dep_USD_`win' hfps_TOT_dep_box_`win'  ms_TOT_dep_box_`win' {
	
	local append_replace="append"
	if "`takeup_measure'"=="lb_total_amt_dep_USD_`win'" { 
	local append_replace="replace"
	}
	
	*1. Base regression for main treatment effect
	reg `takeup_measure'  multi_box `regressors' if (one_box==1 | multi_box==1) & id!=id[_n-1] 
	sum `takeup_measure' if e(sample) & one_box==1
	local mean=r(mean)
	local sd=r(sd)
	outreg2 using "${path2}\table_3_box.out", `append_replace'  ///
	nonote se symb(***,**,*)  bdec(2) nocons addstat("mean in 1 box group", `mean', "sd in 1 box group", `sd') ///
	drop(bl_age_10 bl_years_educ bl_kids_u1 bl_sav10_box_dum bl_sav10_bank_dum bl_has_mobile_money)
	}
	
		
*Airtel regressions


foreach takeup_measure of varlist at_total_dep_amt_may18_USD_`win' hfps_TOT_dep_mm_`win' ms_TOT_dep_mm_`win' {

	local append_replace="append"	
	if "`takeup_measure'"=="at_total_dep_amt_may18_USD_`win'" { 
	local append_replace="replace"
	}
	
	*1. Base regression for main treatment effect
	reg `takeup_measure' multi_mm `regressors' if (one_mm==1 | multi_mm==1) & id!=id[_n-1]
	sum `takeup_measure' if e(sample) & one_mm==1 & id!=id[_n-1]
	local mean=r(mean)
	local sd=r(sd)
	outreg2 using "${path2}\table_3_mm.out", `append_replace'  ///
	 nonote se symb(***,**,*)  bdec(2) nocons addstat("mean in 1 mm group", `mean', "sd in 1 mm group", `sd') ///
	 drop(bl_age_10 bl_years_educ bl_kids_u1 bl_sav10_box_dum bl_sav10_bank_dum bl_has_mobile_money bl_animal_durable_assets_w5_mdum)
	 
	}
	
	

************************************************
*Figure 1. Cumulative distributions of deposits
************************************************

sum hfps_TOT_dep_box if id!=id[_n-1] & box==1, d
local p95_hfps=r(p95)
cumul hfps_TOT_dep_box if id!=id[_n-1] & one_box==1, gen(cdf_box_HFPS_1)
cumul hfps_TOT_dep_box if id!=id[_n-1] & multi_box==1, gen(cdf_box_HFPS_m)


sum ms_TOT_dep_box if id!=id[_n-1] & box==1, d
local p95_ms=r(p95)
cumul ms_TOT_dep_box if id!=id[_n-1] & one_box==1, gen(cdf_box_MS_1)
cumul ms_TOT_dep_box if id!=id[_n-1] & multi_box==1, gen(cdf_box_MS_m)

************************************************
*Figure 1. *Panel A1. CDF of deposits in HFPS
************************************************

*#delimit
sum hfps_TOT_dep_box if id!=id[_n-1] & box==1, d
local p95_hfps=r(p95)

sum ms_TOT_dep_box if id!=id[_n-1] & box==1, d
local p95_ms=r(p95)

twoway ///
 (scatter cdf_box_HFPS_1 hfps_TOT_dep_box if hfps_TOT_dep_box<`p95_hfps' & one_box==1, msymbol(X) mcolor(gs10)) ///
 (scatter cdf_box_HFPS_m hfps_TOT_dep_box if hfps_TOT_dep_box<`p95_hfps' & multi_box==1, msymbol(o) mcolor(black)), ///
 legend(order(1 "One box" 2 "Multiple boxes" ) rows(2)) ///
 graphregion(color(white)) ///
 ytitle("% at or below") xtitle("Value of deposits recorded in HFPS (USD)") title("CDF of deposits in HFPS, Multiple vs. Single accounts", size(med))
 
graph export "${path2}\figure1_panelA1.png", replace


***********************************************************************
*Figure 1. *Panel B1. Value of deposits recorded in monitoring surveys
***********************************************************************

twoway ///
 (scatter cdf_box_MS_1 ms_TOT_dep_box if ms_TOT_dep_box<`p95_ms' & one_box==1, msymbol(X) mcolor(gs10)) ///
 (scatter cdf_box_MS_m ms_TOT_dep_box if ms_TOT_dep_box<`p95_ms' & multi_box==1, msymbol(o) mcolor(black)) , ///
 legend(order(1 "One box" 2 "Multiple boxes" ) rows(2)) ///
 graphregion(color(white)) ///
 ytitle("% at or below") xtitle("Value of deposits recorded in monitoring surveys (USD)") title("CDF of deposits in Monitoring Surveys, Multiple vs. Single accounts", size(med))
 
graph export "${path2}\figure1_panelB1.png", replace



*************************************************************
*Figure 1. *Panel C1. CDF of balance in box at Lockbox check
*************************************************************

*#delimit 
*Panel B. Balances
drop cdf_*
cumul lb_balance_box_dec17_USD_`win' if id!=id[_n-1] & one_box==1, gen(cdf_lb_check_1)
cumul lb_balance_box_dec17_USD_`win' if id!=id[_n-1] & multi_box==1, gen(cdf_lb_check_2)
*#delimit
sum lb_balance_box_dec17_USD_`win' if id!=id[_n-1], d
local p95_lb=r(p95)


twoway ///
 (scatter cdf_lb_check_1 lb_balance_box_dec17_USD if lb_balance_box_dec17_USD_<`p95_lb' & one_box==1, msymbol(X) mcolor(gs10)) ///
 (scatter cdf_lb_check_2 lb_balance_box_dec17_USD if lb_balance_box_dec17_USD_<`p95_lb' & multi_box==1, msymbol(o) mcolor(black)), ///
  legend(order(1 "One box" 2 "Multiple boxes") rows(2)) ///
 graphregion(color(white)) ///
 ytitle("% at or below") xtitle("Balance in box at lockbox check (USD)") title("CDF of balance in box at Lockbox check", size(med))
 
graph export "${path2}\figure1_panelC1.png", replace


**************************************************************************
*Figure 1. *Panel A2. CDF of MM deposits in High Frequency Phone Surveys
**************************************************************************

*#delimit
sort id
cumul hfps_TOT_dep_mm if id!=id[_n-1] & (one_mm==1 ), gen(cdf_mm_HFPS_1mm)
cumul hfps_TOT_dep_mm if id!=id[_n-1] & (multi_mm==1), gen(cdf_mm_HFPS_2mm)

sum hfps_TOT_dep_mm if id!=id[_n-1] & one_mm==1, d
local p95_1=r(p95)
sum hfps_TOT_dep_mm if id!=id[_n-1] & multi_mm==1, d
local p95_2=r(p95)

twoway ///
 (scatter cdf_mm_HFPS_1mm hfps_TOT_dep_mm if hfps_TOT_dep_mm<`p95_1' & (one_mm==1 ), msymbol(X) mcolor(gs10)) ///
 (scatter cdf_mm_HFPS_2mm hfps_TOT_dep_mm if hfps_TOT_dep_mm<`p95_2' & (multi_mm==1 ), msymbol(o) mcolor(black)) , ///
 legend(order(1 "1 Mobile Money" 2 "2 Mobile Money Accounts") rows(2)) ///
 graphregion(color(white)) ///
 ytitle("% at or below") xtitle("Value of deposits recorded in HFPS (USD)") title("CDF of MM deposits in High Frequency Phone Surveys", size(med)) ///
 ylabel(0 (.2) 1)
 graph export "${path2}\figure1_panelA2.png", replace

 
**************************************************************************
*Figure 1. *Panel B2. Value of deposits recorded in monitoring surveys
**************************************************************************
 
* #delimit
cumul ms_TOT_dep_mm_`win' if id!=id[_n-1] & (one_mm==1 ), gen(cdf_mm_MS_1mm)
cumul ms_TOT_dep_mm_`win' if id!=id[_n-1] & (multi_mm==1 ), gen(cdf_mm_MS_2mm)

sum ms_TOT_dep_mm if id!=id[_n-1] & (one_mm==1), d
local p95_1=r(p95)

sum ms_TOT_dep_mm if id!=id[_n-1] & (multi_mm==1), d
local p95_2=r(p95)

twoway ///
 (scatter cdf_mm_MS_1mm ms_TOT_dep_mm if ms_TOT_dep_mm<`p95_1' & (one_mm==1), msymbol(X) mcolor(gs10)) ///
 (scatter cdf_mm_MS_2mm ms_TOT_dep_mm if ms_TOT_dep_mm<`p95_2' & (multi_mm==1), msymbol(o) mcolor(black)), ///
 legend(order(1 "1 Mobile Money" 2 "2 Mobile Money Accounts") rows(2)) ///
 graphregion(color(white)) ///
 ytitle("% at or below") xtitle("Value of deposits recorded in monitoring surveys (USD)") title("CDF of MM deposits in Monitoring Surveys", size(med)) ///
 ylabel(0 (.2) 1)
  graph export "${path2}\figure1_panelB2.png", replace
 
 drop cdf_mm_*
 
**********************************************************************
*Figure 1. *Panel C2. Value of deposits in Airtel administrative data
**********************************************************************

*#delimit
local win w5
sort id
cumul at_total_dep_amt_aug19_USD_`win' if id!=id[_n-1] & (one_mm==1) , gen(cdf_mm_airtel_1mm)
cumul at_total_dep_amt_aug19_USD_`win' if id!=id[_n-1] & (multi_mm==1) , gen(cdf_mm_airtel_2mm)
*#delimit
sum at_total_dep_amt_may18_USD if id!=id[_n-1], d
local p95=r(p95)
*#delimit
twoway ///
 (scatter cdf_mm_airtel_1mm at_total_dep_amt_aug19_USD if at_total_dep_amt_aug19_USD<`p95' & (one_mm==1 )  , msymbol(X) mcolor(gs10)) ///
 (scatter cdf_mm_airtel_2mm at_total_dep_amt_aug19_USD if at_total_dep_amt_aug19_USD<`p95' & ( multi_mm==1)  , msymbol(o) mcolor(black)), ///
 legend(order(1 "1 Mobile Money Account" 2 "2 Mobile Money Accounts") rows(2)) ///
 graphregion(color(white)) ///
 ytitle("% at or below") xtitle("Value of deposits in Airtel administrative data (USD)") title("CDF of deposits in Airtel administrative data", size(med)) ///
 ylabel(0 (.2) 1)
  graph export "${path2}\figure1_panelC2.png", replace
  

  
******************************************************************
*Appendix Figure A4: Mobile money usage over time
******************************************************************

*#delimit
use "${path1}\Airtel_transactions_ID_date.dta", clear	

foreach var of varlist ///
cashin_amt cashout_amt recharge_amt merchant_amt recharge_o_amt rollback_amt ///
Ent2Reg_amt p2p_sent_amt p2p_rec_amt loan_disburse_amt loan_payment_amt autoloan_payment_amt ///
bank_wallet_tr_amt cnncreq_amt wallet_churn_amt trans_correction_amt fee_amt{

bysort actual_date_r: egen total_date_`var'=total(`var')
replace total_date_`var'=1 if total_date_`var'>=1
}

egen non_missing_date=rowtotal(total_date_*)
replace non_missing_date=1 if non_missing_date>1

*deposits and withdrawals

	collapse (mean) *_amt *_num *_dum running_balance_w5  non_missing_date, by(actual_date_r)

	sum actual_date_r if non_missing==0
	local min=r(min)
	local max=r(max)
	
	tab actual_date if actual_date==`min'	
	tab actual_date if actual_date==`max'	
		
*GRAPHS:
	*Panel A. Cumulative usage: Deposits and Withdrawals
	
		sort actual_date_r
		gen cumul_cashin_amt=sum(cashin_amt)
		gen cumul_cashout_amt=sum(cashout_amt)
		gen cumul_p2p_rec_amt=sum(p2p_rec_amt)
		gen net_deposits=cumul_cashin_amt-cumul_cashout_amt
		
		graph twoway  ///
		(line cumul_cashin_amt actual_date_r if actual_date_r<=`min', color(dknavy) ) ///
		(line cumul_cashin_amt actual_date_r if actual_date_r>=`max', color(dknavy) ) ///
		(line cumul_cashout_amt actual_date_r if actual_date_r<=`min', color(red)) ///
		(line cumul_cashout_amt actual_date_r if actual_date_r>=`max', color(red)) ///
		(line net_deposits actual_date_r if actual_date_r<=`min', color(green)) ///
		(line net_deposits actual_date_r if actual_date>=`max', color(green)) ///
		(pcarrowi   150 21325 150 21315 (3) "Last Reimbursement", color(black) mlabcolor(black)) ///
		, xline(21311, lpattern(dash) lcolor(nblue)) graphregion(color(white)) ///
		legend(lab(1 "Total Deposits") lab(3 "Total Withdrawals") lab(5 "Net Deposits") order(1 3 5)) ///
		ytitle("Average per Day Across Users, in USD") xtitle(Date) ///
		xlabel( ///
		21001   "1July2017" ///
		21063	"1Sep2017" ///
		21124	"1Nov2017" ///
		21185	"1Jan2018" ///
		21244	"1Mar2018" ///
		21305	"1May2018" ///
		21366	"1Jul2018" ///
		21428	"1Sep2018" ///
		21489	"1Nov2018" ///
		21550	"1Jan2019" ///
		21609	"1Mar2019" ///
		21670	"1May2019" ///
		21731	"1Jul2019", angle(vertical) ) /*					21311 	"7May2018"*/

		graph export "${path2}\\figA4_panelA.png", replace		
	
	*Panel C. Running Balance (averages per day, smoothed over 2 week period)
	
		graph twoway ///
		(line running_balance_w5 actual_date_r if actual_date_r<=`min', color(dknavy)) ///
		(line running_balance_w5 actual_date_r if actual_date_r>=`max', color(dknavy)) ///
		(pcarrowi   4 21325 4 21315 (3) "Last Reimbursement", color(black) mlabcolor(black)) ///
		, xline(21311, lpattern(dash) lcolor(black)) graphregion(color(white)) legend(off) ///
		ytitle("Average Balance per Day Across Users, in USD") xtitle(Date) ///
		xlabel( ///
		21001   "1July2017" ///
		21063	"1Sep2017" ///
		21124	"1Nov2017" ///
		21185	"1Jan2018" ///
		21244	"1Mar2018" ///
		21305	"1May2018" ///
		21366	"1Jul2018" ///
		21428	"1Sep2018" ///
		21489	"1Nov2018" ///
		21550	"1Jan2019" ///
		21609	"1Mar2019" ///
		21670	"1May2019" ///
		21731	"1Jul2019" ///
		, angle(vertical) )
		
		graph export "${path2}\\figA4_panelC.png", replace	

					
	*Panel B. Deposits and Withdrawals (averages per day, smoothed over 2 week period)
	
		gen actual_date_center=actual_date_r-21315
		gen date_center=int(actual_date_center/14)
		replace date_center=date_center-1 if actual_date_r<21315
		
		drop if actual_date_r>=`min' & actual_date_r<=`max'
		
		collapse (mean) *_amt *_num *_dum running_balance_w5  (min) actual_date_r, by(date_center)
		
		*amount of deposits per day
		graph twoway  ///
		(line  cashin_amt actual_date_r if actual_date_r<=`min', color(blue)) ///
		(line  cashin_amt actual_date_r if actual_date_r>=`max', color(blue)) ///
		(line  cashout_amt actual_date_r if actual_date_r<=`min', color(red) ) ///
		(line  cashout_amt actual_date_r if actual_date_r>=`max', color(red) ) ///
		(pcarrowi   0.5 21325 0.5 21315 (3) "Last Reimbursement", color(black) mlabcolor(black)) ///
		, xline(21311, lpattern(dash) lcolor(black)) graphregion(color(white)) legend(lab(1 "Deposits") /// 
		lab(3 "Withdrawals") order( 1 3) ) ytitle("Averages Per Day") xtitle(Date) ///
		xlabel( ///
		21001   "1July2017" ///
		21063	"1Sep2017" ///
		21124	"1Nov2017" ///
		21185	"1Jan2018" ///
		21244	"1Mar2018" ///
		21305	"1May2018" ///
		21366	"1Jul2018" ///
		21428	"1Sep2018" ///
		21489	"1Nov2018" ///
		21550	"1Jan2019" ///
		21609	"1Mar2019" ///
		21670	"1May2019" ///
		21731	"1Jul2019" ///
		, angle(vertical) )

		graph export "${path2}\\figA4_panelB.png", replace	
 
  
  
************************
*Table A1: Saving Goals
************************

use "${path1}MAP2_Working_File.dta", clear
*Baseline goals
egen bl_goal_kids_school=rmax(bl_sav12*pur_1)
egen bl_goal_agriculture=rmax(bl_sav12*pur_3)
egen bl_goal_business=rmax(bl_sav12*pur_4 bl_sav12*pur_5 bl_sav12*pur_22)
egen bl_goal_homeimprove=rmax(bl_sav12*pur_6)
egen bl_goal_durable=rmax(bl_sav12*pur_7)
egen bl_goal_health=rmax(bl_sav12*pur_8)
egen bl_goal_home_exp=rmax(bl_sav12*pur_10 bl_sav12*pur_19)
egen bl_goal_emer=rmax(bl_sav12*pur_11 bl_sav12*pur_12)
egen bl_goal_consumption=rmax(bl_sav12*pur_13)
egen bl_goal_land=rmax(bl_sav12*pur_14 bl_sav12*pur_17)
egen bl_goal_hungry_seas=rmax(bl_sav12*pur_23)
egen bl_goal_vehicle=rmax(bl_sav12*pur_15 bl_sav12*pur_16)


*Goals for the box
gen pre_box_goal_kids_school=pre_any_account_goal_1 if (one_box==1 | multi_box==1)
gen pre_box_goal_agriculture=pre_any_account_goal_3  if (one_box==1 | multi_box==1)
gen pre_box_goal_business=(pre_any_account_goal_4 ==1 | pre_any_account_goal_5==1 | pre_any_account_goal_22==1)  if (one_box==1 | multi_box==1)
gen pre_box_goal_homeimprove=pre_any_account_goal_6  if (one_box==1 | multi_box==1)
gen pre_box_goal_durable=pre_any_account_goal_7  if (one_box==1 | multi_box==1)
gen pre_box_goal_health=pre_any_account_goal_8  if (one_box==1 | multi_box==1)
gen pre_box_goal_home_exp=(pre_any_account_goal_19==1 |  pre_any_account_goal_10==1)  if (one_box==1 | multi_box==1)
gen pre_box_goal_emer=(pre_any_account_goal_12==1 | pre_any_account_goal_11==1)  if (one_box==1 | multi_box==1)
gen pre_box_goal_consumption = (pre_any_account_goal_13) if (one_box==1 | multi_box==1)
gen pre_box_goal_land =(pre_any_account_goal_17==1 | pre_any_account_goal_14 ==1) if (one_box==1 | multi_box==1)
gen pre_box_goal_hungry_seas=pre_any_account_goal_23  if (one_box==1 | multi_box==1)
gen pre_box_goal_vehicle =(pre_any_account_goal_15==1 | pre_any_account_goal_16==1)  if (one_box==1 | multi_box==1)

*Goals for the MM

gen pre_mm_goal_kids_school=pre_any_account_goal_1 if mobile_money==1
gen pre_mm_goal_agriculture=pre_any_account_goal_3  if mobile_money==1
gen pre_mm_goal_business=(pre_any_account_goal_4 ==1 | pre_any_account_goal_5==1 | pre_any_account_goal_22==1)  if mobile_money==1
gen pre_mm_goal_homeimprove=pre_any_account_goal_6  if mobile_money==1
gen pre_mm_goal_durable=pre_any_account_goal_7  if mobile_money==1
gen pre_mm_goal_health=pre_any_account_goal_8  if mobile_money==1
gen pre_mm_goal_home_exp=(pre_any_account_goal_19==1 |  pre_any_account_goal_10==1)  if mobile_money==1
gen pre_mm_goal_emer=(pre_any_account_goal_12==1 | pre_any_account_goal_11==1)  if mobile_money==1
gen pre_mm_goal_consumption = (pre_any_account_goal_13) if mobile_money==1
gen pre_mm_goal_land =(pre_any_account_goal_17==1 | pre_any_account_goal_14 ==1) if mobile_money==1
gen pre_mm_goal_hungry_seas=pre_any_account_goal_23  if mobile_money==1
gen pre_mm_goal_vehicle =(pre_any_account_goal_15==1 | pre_any_account_goal_16==1)  if mobile_money==1

**********************************
*1: Baseline Goals for All
**********************************

local varlist  bl_goal_business bl_goal_consumption bl_goal_emer ///
 bl_goal_land bl_goal_durable bl_goal_kids_school bl_goal_homeimprove bl_goal_home_exp bl_goal_agriculture 

tokenize `varlist'
local goalvars=wordcount("`varlist'")

sort id
gen var=""
for any mean sd N: gen X=.
local i 1
while `i' <= `goalvars' {
	
	sum ``i'' if id!=id[_n-1] & (box==1 |mobile_money==1 | control==1)
	replace var="``i''" if _n==`i'
	replace mean=r(mean) if _n==`i'
	replace N=r(N) if _n==`i'
	local i = `i' + 1
	}

outsheet var mean N using "${path2}\tableA1_baseline_goals_all.out" if _n<=`i', replace  

********************************
*2: Saving Goals for Box
********************************
	 
local varlist pre_box_goal_business pre_box_goal_consumption pre_box_goal_emer ///
 pre_box_goal_land pre_box_goal_durable pre_box_goal_kids_school pre_box_goal_homeimprove pre_box_goal_home_exp pre_box_goal_agriculture 

tokenize `varlist'
local goalvars=wordcount("`varlist'")

drop var mean sd N
sort id
gen var=""
for any mean sd N: gen X_1box=. \ gen X_multibox=.
local i 1
while `i' <= `goalvars' {

	replace var="``i''" if _n==`i'
	
	sum ``i'' if id!=id[_n-1] & (one_box==1)
	replace mean_1box=r(mean) if _n==`i'
	replace N_1box=r(N) if _n==`i'

	sum ``i'' if id!=id[_n-1] & (multi_box==1)
	replace mean_multibox=r(mean) if _n==`i'
	replace N_multibox=r(N) if _n==`i'
	
	local i = `i' + 1
	}

outsheet var mean_1box N_1box mean_multibox N_multibox using "${path2}\tableA1_box_goals.out" if _n<=`i', replace  
drop var mean* sd* N*


********************************
*3: Saving Goals for MM
********************************
	 
	 
local varlist pre_mm_goal_business pre_mm_goal_consumption pre_mm_goal_emer pre_mm_goal_land ///
 pre_mm_goal_durable pre_mm_goal_kids_school pre_mm_goal_homeimprove pre_mm_goal_home_exp pre_mm_goal_agriculture

tokenize `varlist'
local goalvars=wordcount("`varlist'")

sort id
gen var=""
for any mean sd N: gen X_1mm=. \ gen X_multimm=.
local i 1
while `i' <= `goalvars' {

	replace var="``i''" if _n==`i'
	
	sum ``i'' if id!=id[_n-1] & (one_mm==1)
	replace mean_1mm=r(mean) if _n==`i'
	replace N_1mm=r(N) if _n==`i'

	sum ``i'' if id!=id[_n-1] & (multi_mm==1)
	replace mean_multimm=r(mean) if _n==`i'
	replace N_multimm=r(N) if _n==`i'
	
	local i = `i' + 1
	}

outsheet var mean_1mm N_1mm mean_multimm N_multimm using "${path2}\tableA1_mm_goals.out" if _n<=`i', replace 



****************************
*Table A2. Attrition
****************************

use "${path1}MAP2_Working_File.dta", clear


preserve
*HFPS
sort id hfps_round hfps_call_date_raw
drop if id==id[_n-1] & hfps_round==hfps_round[_n-1] & hfps_call_date_raw==hfps_call_date_raw[_n-1]

by id hfps_round: egen hfps_num_surveys=count(hfps_call_date_raw)

sort hfps_round id
sum hfps_num_surveys if hfps_round==1 & id!=id[_n-1] & hfps==1, d
sum hfps_num_surveys if hfps_round==2 & id!=id[_n-1] & hfps==1, d

gen x=(hfps_num_surveys>0) if hfps_round==1 & hfps==1
bysort id: egen hfps_atleast1_round1=max(x) 

gen x2=(hfps_num_surveys>0) if hfps_round==2 & hfps==1
bysort id: egen hfps_atleast1_round2=max(x2) if hfps==1

replace x=hfps_num_surveys/16 if hfps_round==1
egen hfps_per_complete_round1=max(x) if hfps==1, by(id)

replace x2=hfps_num_surveys/6 if hfps_round==2
egen hfps_per_complete_round2=max(x2) if hfps==1, by(id)

gen behavioral_boxes_sample=1 if one_box==1 | multi_box==1 | control==1
gen mobile_money_sample=1 if one_mm==1 | multi_mm==1 | control==1


*Checks if some have more than the max number of surveys
for num 1/2: replace hfps_per_complete_roundX=1 if hfps_per_complete_roundX>1 & hfps_per_complete_roundX!=.
 
sort id
keep if id!=id[_n-1]
keep id hfps_atleast1_round1 hfps_atleast1_round2 hfps_per_complete_round1 hfps_per_complete_round2
tempfile temp
save `temp', replace

*Monitoring surveys
restore

egen x=rownonmiss(ms_*) if ms_round==1, strok
replace x=0 if x==.
replace x=1 if x>0 & x!=.
egen did_ms1=max(x), by(id)
drop x

egen x=rownonmiss(ms_*) if ms_round==2, strok
replace x=0 if x==.
replace x=1 if x>0 & x!=.
egen did_ms2=max(x), by(id)
drop x

sort id
keep if id!=id[_n-1]
keep id did_ms1 did_ms2 one_box multi_box one_mm multi_mm control hfps one_mm multi_mm
merge 1:1 id using `temp'
*erase temp.dta


******************
*Run regressions
******************

gen behavioral_boxes_sample=1 if one_box==1 | multi_box==1 | control==1
gen mobile_money_sample=1 if one_mm==1 | multi_mm==1 | control==1
	
gen box=(one_box==1 | multi_box==1)

gen hfps_box=hfps*box
gen hfps_mm=hfps*mobile_money

local append_replace replace

foreach var of varlist  hfps_atleast1_round1 hfps_per_complete_round1 hfps_atleast1_round2 hfps_per_complete_round2{

	*1. Behavioral boxes + MM
	reg `var' one_box multi_box one_mm multi_mm hfps  if id!=id[_n-1] & ( behavioral_boxes_sample==1 | mobile_money_sample==1)
	
	sum `var' if control==1 & hfps==1 & e(sample)
	local mean=r(mean)
	local sd=r(sd)
	
	outreg2 using "${path2}\tableA2_attrition_mm_boxes.out", `append_replace' nonote se symb(***, **, *) dec(2) nocons ///
	 addstat("control mean", `mean', "control sd", `sd')
		 
	local append_replace append

	 }

	 
*Monitoring surveys
foreach var of varlist   did_ms1  did_ms2{

	*1. Behavioral boxes + MM
	reg `var' one_box multi_box one_mm multi_mm hfps  if id!=id[_n-1] & ( behavioral_boxes_sample==1 | mobile_money_sample==1)
		
	sum `var' if control==1 & hfps==0 & e(sample)
	local mean=r(mean)
	local sd=r(sd)
	
	outreg2 using "${path2}\tableA2_attrition_mm_boxes.out", append nonote se symb(***, **, *) dec(2) nocons ///
	 addstat("control mean", `mean', "control sd", `sd') ///
	
	}
	


************************************************************************************
*Table A10: Usage of Mobile Money Table With Airtel Admin Data
************************************************************************************

*#delimit
use "${path1}MAP2_Working_File.dta", clear


* Usage of Mobile Money Accounts - Descriptive

*what the MM accounts were used for
	gen ms_mmgl_any_goal_all_mm=0 if !missing(ms_mmgl_goal1)
	replace ms_mmgl_any_goal_all_mm=1 if ms_mmgl_goal1==1 | ms_mmgl_goal2_silver==1 | ms_mmgl_goal2==1 | ms_mmgl_goal2_black==1
	lab var ms_mmgl_any_goal_all_mm "Was saving for a particular goal in MM accounts"

	gen ms_mmgl_achieveatleast1_all_mm=0 if !missing(ms_mmgl_achieve_silver)
	replace ms_mmgl_achieveatleast1_all_mm=1 if ms_mmgl_achieve_silver==1 | ms_mmgl_achieve2_silver==1 | ms_mmgl_achieve_black==1 | ms_mmgl_achieve2_black==1
	lab var ms_mmgl_achieveatleast1_all_mm "Achieved at least 1 saving goals"

	gen ms_mmgl_use_all_mm_1=0 if !missing(ms_mmgl_use_silver_1)
	replace ms_mmgl_use_all_mm_1=1 if ms_mmgl_use_black_1==1 | ms_mmgl_use_silver_1==1
	lab var ms_mmgl_use_all_mm_1 "Used Account for Saving"
	
	gen ms_mmgl_use_all_mm_2=0 if !missing(ms_mmgl_use_silver_1)
	replace ms_mmgl_use_all_mm_2=1 if ms_mmgl_use_black_2==1 | ms_mmgl_use_silver_2==1
	lab var ms_mmgl_use_all_mm_2 "Used Account To store money temporarily"


	local varlist ms_mmgl_use_all_mm_1 	ms_mmgl_use_all_mm_2 ///

	tokenize `varlist'
	local goalvars=wordcount("`varlist'")

	keep if ms_round==2
	sort id
	gen var=""
	for any mean sd N: gen X=.
	local i 1
	while `i' <= `goalvars' {

		
		sum ``i''  if (one_mm==1 | multi_mm==1) & id!=id[_n+1] 
		replace var="``i''" if _n==`i'
		replace mean=r(mean) if _n==`i'
		replace N=r(N) if _n==`i'
		local i = `i' + 1
		}

	outsheet var mean N using "${path2}\tableA10_mm_usage_descriptive.out" if _n<=`i', replace  

	
************************************************
*Figuring out what people used MM for (Summary)
************************************************	

use "${path1}\Airtel_transactions_ID_date.dta", clear
	
*preserve
	keep if actual_date_r<=21311 /*keeping transactions for project durations*/
	tab actual_date if actual_date==21311
	
	
	egen total_tr=rowtotal(*amt)
	bysort id: egen total_usage=total(total_tr)
	drop total_tr
	gen used_at_all=(total_usage>0)
	
	egen transaction_bills=rowtotal(recharge_amt merchant_amt bank_wallet_tr_amt	wallet_churn_amt)
	
	*used for paying bills
	gen used_for_transactions=0
	foreach var of varlist ///
	recharge_amt 			merchant_amt ///
	recharge_o_amt 			rollback_amt ///
	bank_wallet_tr_amt 		wallet_churn_amt transaction_bills{
		bysort id: egen	total_`var'=total(`var')		
		replace used_for_transactions=1 if total_`var'>0		
	}
	
	gen transactions_bills_bin=total_transaction_bills
	replace transactions_bills_bin=1 if transactions_bills_bin>0 & !missing(transactions_bills_bin)
	
	*used for p2p
	gen used_p2p=0	
	foreach var of varlist p2p_sent_amt p2p_rec_amt{
		bysort id: egen total_`var'=total(`var')
		gen `var'_bin=(total_`var'>0)
		replace used_p2p=1 if total_`var'>0
		replace used_at_all=1 if total_`var'>0	
	}

	*used only to deposit and withdraw money
	gen used_in_out=0
	foreach var of varlist cashin_amt cashout_amt {
	bysort id: egen total_`var'=total(`var')
	gen `var'_bin=(total_`var'>0)
	replace used_in_out=1 if total_`var'>0
	replace used_at_all=1 if total_`var'>0	
	}

	keep id used* total* *bin   
	drop total_recharge_amt total_merchant_amt total_recharge_o_amt total_rollback_amt total_bank_wallet_tr_amt total_wallet_churn_amt	
	duplicates drop
	

	*winsorizing
	local win w5
	local 100minuswin=95
	foreach var in  total_p2p_sent_amt total_p2p_rec_amt total_cashin_amt total_cashout_amt total_usage total_transaction_bills{
		gen `var'_`win'=`var'
		qui sum `var' if `var'!=0, d
		replace `var'_`win'=r(p`100minuswin') if `var'>r(p`100minuswin') & !missing(`var')
		}
	
	*Make sum stats table on usage
	gen description=""
	for any mean p50 sd N: gen X_overall=.  

	local i 1
	foreach takeup_measure of varlist used_in_out total_cashin_amt_`win' total_cashout_amt_`win' ///
	used_p2p  total_p2p_sent_amt_`win' total_p2p_rec_amt_`win'  transactions_bills_bin total_transaction_bills_`win' {
	replace `takeup_measure'=0 if `takeup_measure'==.
	quietly sum `takeup_measure' if used_at_all>0, d
	quietly replace mean_overall=r(mean) if _n==`i'
	quietly replace description="`takeup_measure'" if _n==`i'
	quietly replace sd_overall=r(sd) if _n==`i'
	quietly replace p50_overall=r(p50) if _n==`i'
	quietly replace N_overall=r(N) if _n==`i'

	local i = `i' + 1
	}
	local N=`i'-1

	outsheet description mean_overall sd_overall p50_overall N_overall ///
	using "${path2}\tableA10_usage_mm_sum_stats.out" if _n<=`N', replace

  
  
  
********************************************************************************************************************
*Treatment effects on deposits, withdrawals and balances; credit; labor supply and business outcomes; expenditures; 
*                     treating illness, prevalence of sickness; long-term usage of accounts
***************************************************  ***************************************************************

***************************************************
*TABLES 4-9, APPENDIX A7-A9, A12-A14, B1-B5, E1-E8 
***************************************************
*#delimit

use "${path1}MAP2_Working_File.dta", clear
gen hfps_group=(bl_group<=5)
gen hfps_box=hfps_group*box
gen hfps_mobile_money=hfps_group*mobile_money
gen one_box_hfps=one_box*hfps
gen multi_box_hfps=multi_box*hfps
gen one_mm_hfps=one_mm*hfps
gen multi_mm_hfps=multi_mm*hfps

*heterogeneity for MM

replace bl_q35_mm_dist_owner_min_q=bl_q35_mm_dist_owner_min_q-1
sum bl_q35_mm_dist_owner_min_q
rename bl_q35_mm_dist_owner_min_q agent_distance
gen mm_agent_distance=mobile_money*agent_distance


local win w5
local 100minuswin=95

 
 rename *_r_calc* *_today*
 drop  hfps_exp_tot_today

******************
*Generate dependent variables, and pre-treatment controls from the intake survey, for ANCOVA analysis
******************

*****************************************************************************
************************SAVINGS  OUTCOMES************************************ 
*****************************************************************************

**********
*Deposits
**********

*Total deposits in IPA boxes
egen hfps_d_dep_box_IPA_tot_week=rowtotal(hfps_d_dep_box_silver_week hfps_d_dep_box_black_week hfps_d_dep_box_brown_week), missing
gen pre_d_dep_box_IPA_tot_week =0 

*Total deposits in non-IPA boxes
egen pre_d_dep_nonIPA_box_week=rowtotal(pre_d_dep_box_week ), missing	
gen pre_d_dep_box_ALL_2m_tot_bin=(pre_d_dep_nonIPA_box_week>0 ) & !missing(pre_d_dep_nonIPA_box_week)

*Total deposits into Boxes
egen hfps_d_dep_box_ALL_tot_week=rowtotal(hfps_d_dep_box_IPA_tot_week hfps_d_dep_nonIPA_box_week), missing
gen pre_d_dep_box_ALL_tot_week=pre_d_dep_nonIPA_box_week
gen pre_d_dep_box_ALL_tot_week_bin=(pre_d_dep_box_ALL_tot_week>0) & !missing(pre_d_dep_box_ALL_tot_week)

		
*Total deposits in ROSCAs and VSLAs
egen hfps_d_dep_svgroup_tot_week=rowtotal(hfps_d_dep_vsla_week hfps_d_dep_rosca_week), missing
egen ms_d_dep_svgroup_tot_2m=rowtotal(ms_d_dep_vsla_2m_total ms_d_dep_rosca_2m_total), missing
egen pre_d_dep_svgroup_tot_week=rowtotal(pre_d_dep_vsla_week pre_d_dep_rosca_week), missing


*Total deposits in IPA mobile money
egen hfps_d_dep_mm_IPA_tot_week=rowtotal(hfps_d_dep_mm1_week hfps_d_dep_mm2_week), missing
gen pre_d_dep_mm_IPA_tot_week =0 


*Total Deposits into IPA MM or IPA Boxed
egen hfps_d_dep_mmboxIPA_tot_week=rowtotal(hfps_d_dep_mm_IPA_tot_week hfps_d_dep_box_IPA_tot_week), missing
gen pre_d_dep_mmboxIPA_tot_week=0


egen ms_d_dep_mmboxIPA_2m_tot=rowtotal(ms_d_dep_box_ipa_2m_total ms_d_dep_mm_ipa_2m_total), missing

*Total Deposits into MM
egen hfps_d_dep_mm_ALL_tot_week=rowtotal(hfps_d_dep_mm_IPA_tot_week hfps_d_dep_nonIPA_mm_week)
gen pre_d_dep_mm_ALL_tot_week = pre_d_dep_mm_tot_week
gen pre_d_dep_mm_ALL_2m_tot_bin =(pre_d_dep_mm_ALL_tot_week>0 ) & !missing(pre_d_dep_mm_ALL_tot_week)
gen pre_d_dep_mm_ALL_tot_week_bin=pre_d_dep_mm_ALL_2m_tot_bin

*Mobile money
gen pre_d_dep_nonIPA_mm_week=pre_d_dep_mm_tot_week

*Other sources
gen pre_d_dep_other_week =pre_d_dep_othersp_week
	
*Total deposits in all sources
egen hfps_d_dep_TOTAL_week=rowtotal(hfps_d_dep_home_week hfps_d_dep_box_silver_week  hfps_d_dep_box_black_week  hfps_d_dep_box_brown_week ///
 hfps_d_dep_nonIPA_box_week hfps_d_dep_nonIPA_mm_week  hfps_d_dep_bank_week hfps_d_dep_vsla_week  hfps_d_dep_rosca_week hfps_d_dep_other_week hfps_d_dep_mm_IPA_tot_week), missing		
gen pre_d_dep_TOTAL_week=pre_d_dep_tot_week
gen pre_d_dep_TOTAL=pre_d_dep_TOTAL_week

*MM/Box usage binary
gen hfps_d_dep_box_ALL_tot_week_bin=(hfps_d_dep_box_ALL_tot_week>0) if !missing(hfps_d_dep_box_ALL_tot_week)
gen hfps_d_dep_mm_ALL_tot_week_bin=(hfps_d_dep_mm_ALL_tot_week>0) if !missing(hfps_d_dep_mm_ALL_tot_week)

*Deposits into IPA and non-IPA boxes
egen ms_d_dep_box_ALL_2m_tot=rowtotal(ms_d_dep_box_2m_total ms_d_dep_box_ipa_2m_total), missing
gen ms_d_dep_box_ALL_2m_tot_bin=ms_d_dep_box_ALL_2m_tot
replace ms_d_dep_box_ALL_2m_tot_bin=1 if ms_d_dep_box_ALL_2m_tot>0 & !missing(ms_d_dep_box_ALL_2m_tot)

*Deposits into IPA and non-IPA MM accounts
egen ms_d_dep_mm_ALL_2m_tot=rowtotal(ms_d_dep_mm_2m_tot ms_d_dep_mm_ipa_2m_tot), missing
gen ms_d_dep_mm_ALL_2m_tot_bin=ms_d_dep_mm_ALL_2m_tot
replace ms_d_dep_mm_ALL_2m_tot_bin=1 if ms_d_dep_mm_ALL_2m_tot>0 & !missing(ms_d_dep_mm_ALL_2m_tot)


**********
*Withdrawals
**********

*Total withdrawals in IPA boxes
egen hfps_d_wit_box_IPA_tot_week=rowtotal(hfps_d_wit_box_silver_week hfps_d_wit_box_black_week hfps_d_wit_box_brown_week), missing
gen pre_d_wit_box_IPA_tot_week =0 

*Total withdrawals in non-IPA boxes
egen hfps_d_wit_box_ALL_tot_week=rowtotal(hfps_d_wit_nonIPA_box_week hfps_d_wit_box_IPA_tot_week), missing
gen pre_d_wit_box_ALL_tot_week=pre_d_dep_box_week


*Total withdrawals in ROSCAs and VSLAs (note that this is missing for the MS)
egen hfps_d_wit_svgroup_tot_week=rowtotal(hfps_d_wit_vsla_week hfps_d_wit_rosca_week), missing
egen pre_d_wit_svgroup_tot_week=rowtotal(pre_d_wit_vsla_week pre_d_wit_rosca_week), missing

*Total withdrawals in "other" sources
rename hfps_d_wit_nonIPA_mm_o_week hfps_d_wit_nonIPA_mm_week

*Total withdrawals in IPA mobile money
egen hfps_d_wit_mm_IPA_tot_week=rowtotal(hfps_d_wit_mm1_week hfps_d_wit_mm2_week), missing
gen pre_d_wit_mm_IPA_tot_week =0 

*Total withdrawal in IPA MM or IPA BOX
egen hfps_d_wit_mmboxIPA_tot_week=rowtotal( hfps_d_wit_mm_IPA_tot_week hfps_d_wit_box_IPA_tot_week), missing
gen pre_d_wit_mmboxIPA_tot_week=0

egen ms_d_wit_mmboxIPA_2m_tot=rowtotal(ms_d_wit_box_ipa_2m_total ms_d_wit_mm_ipa_2m_total), missing

*Total withdrawals in any MM
egen hfps_d_wit_mm_ALL_tot_week=rowtotal(hfps_d_wit_mm_IPA_tot_week hfps_d_wit_nonIPA_mm_week ), missing
gen pre_d_wit_mm_ALL_tot_week=pre_d_wit_nonIPA_mm_week

*MM/Box usage binary
gen hfps_d_wit_box_ALL_tot_week_bin=(hfps_d_wit_box_ALL_tot_week>0) if !missing(hfps_d_wit_box_ALL_tot_week)
gen hfps_d_wit_mm_ALL_tot_week_bin=(hfps_d_wit_mm_ALL_tot_week>0) if !missing(hfps_d_wit_mm_ALL_tot_week)

*Other sources
gen pre_d_wit_other_week =pre_d_wit_othersp_week

*Total withdrawals in all sources
egen hfps_d_wit_TOTAL_week=rowtotal(hfps_d_wit_home_week hfps_d_wit_box_silver_week  hfps_d_wit_box_black_week  hfps_d_wit_box_brown_week ///
 hfps_d_wit_nonIPA_box_week hfps_d_wit_nonIPA_mm_week  hfps_d_wit_bank_week hfps_d_wit_vsla_week  hfps_d_wit_rosca_week hfps_d_wit_other_week hfps_d_wit_mm_IPA_tot_week), missing		
egen pre_d_wit_TOTAL_week=rowtotal(pre_d_wit_home_week pre_d_wit_nonIPA_box_week pre_d_wit_bank_week pre_d_wit_othersp_week ///
 pre_d_wit_nonIPA_mm_week pre_d_wit_rosca_week pre_d_wit_vsla_week pre_d_wit_mm_tot_week pre_d_wit_box_IPA_tot_week pre_d_wit_mm_IPA_tot_week), missing
 
*witosits into IPA and non-IPA boxes
egen ms_d_wit_box_ALL_2m_tot=rowtotal(ms_d_wit_box_2m_total ms_d_wit_box_ipa_2m_total), missing
gen ms_d_wit_box_ALL_2m_tot_bin=ms_d_wit_box_ALL_2m_tot
replace ms_d_wit_box_ALL_2m_tot_bin=1 if ms_d_wit_box_ALL_2m_tot>0 & !missing(ms_d_wit_box_ALL_2m_tot)

*witosits into IPA and non-IPA MM accounts
egen ms_d_wit_mm_ALL_2m_tot=rowtotal(ms_d_wit_mm_2m_tot ms_d_wit_mm_ipa_2m_tot), missing
gen ms_d_wit_mm_ALL_2m_tot_bin=ms_d_wit_mm_ALL_2m_tot
replace ms_d_wit_mm_ALL_2m_tot_bin=1 if ms_d_wit_mm_ALL_2m_tot>0 & !missing(ms_d_wit_mm_ALL_2m_tot)


**********
*Balance
**********
*Total balance in IPA boxes
egen ms_d_bal_box_IPA_tot=rowtotal(ms_d_bal_box_silver ms_d_bal_box_black ms_d_bal_box_brown), missing

egen ms_d_bal_box_ALL_tot=rowtotal(ms_d_bal_box ms_d_bal_box_IPA_tot), missing

*Total balance in ROSCAs and VSLAs
egen ms_d_bal_svgroup=rowtotal(ms_d_bal_vsla ms_d_bal_rosca), missing

*Total balance in IPA mm
egen ms_d_bal_mm_IPA_tot=rowtotal(ms_d_bal_mm1 ms_d_bal_mm2), missing

egen ms_d_bal_mm_ALL_tot=rowtotal(ms_d_bal_mm_IPA_tot ms_d_bal_mm), missing

*Total Balance in IPA MM and IPA boxes
egen ms_d_bal_mmboxIPA_tot=rowtotal(ms_d_bal_mm_IPA_tot ms_d_bal_box_IPA_tot), missing

*Total balance in all sources
egen ms_d_bal_TOTAL=rowtotal(ms_d_bal_box_IPA_tot ms_d_bal_box ms_d_bal_home ms_d_bal_bank ms_d_bal_svgroup ms_d_bal_mm ms_d_bal_othersp ///
 ms_d_bal_mm_IPA_tot), missing
 

*replacing balances with missing for Round 1 survey so that the results only show up for round 2
*This is because there was a mistake in the survey and control groups were not asked what their box balances were. Round 2 data is okay 
		
	foreach var of varlist ms*bal* {
	replace `var'=. if ms_round==1
	}
	
 
**********
*New 4/22/19: net withdrawals
**********
foreach X in box_IPA_tot_week nonIPA_box_week home_week bank_week svgroup_tot_week nonIPA_mm_week mm_IPA_tot_week other_week TOTAL_week mmboxIPA_tot_week box_ALL_tot_week mm_ALL_tot_week { 
 gen hfps_d_nd_`X'=hfps_d_dep_`X'-hfps_d_wit_`X' 
 gen pre_d_nd_`X'=pre_d_dep_`X'-pre_d_wit_`X' 
 sum hfps_d_nd_`X',d 
 replace hfps_d_nd_`X'=r(p5) if hfps_d_nd_`X'<r(p5)
  /*winsorizing from the bottom of the distribution*/
}

 *Note: do NOT do this for savings groups since withdrawals were not recorded separately for those
foreach X in box_ipa_2m_total box_2m_total home_2m_total bank_2m_total mm_2m_total othersp_2m_total mm_ipa_total mm_ALL_2m_tot box_ALL_2m_tot mmboxIPA_2m_tot {
 gen ms_d_nd_`X'=ms_d_dep_`X'-ms_d_wit_`X'
 sum ms_d_nd_`X', d
 replace ms_d_nd_`X'=r(p5) if ms_d_nd_`X'<r(p5)
  /*winsorizing from the bottom of the distribution*/
}

 
 
*****************************************************************************
************************BUSINESS OUTCOMES************************************ 
*****************************************************************************

*Generate dependent variables, and pre-treatment controls from the intake survey, for ANCOVA analysis
gen hfps_farm_dum=(hfps_c5_farm_hrs>0)
replace hfps_farm_dum=. if missing(hfps_c5_farm_hrs)
		
replace ms_c5_o_earn_week=ms_c5_o_earn_week/700/*converting to us*/
replace hfps_a3_equip_exp=hfps_a3_equip_exp/700 /*converting to us*/

*Pre-treatment variables, for ANCOVA controls
gen pre_b1_work_prob=pre_b1_worked_main_today /*generating variables that match survey naming*/
gen pre_b2_work_hrs_day=pre_b1_work_hrs
gen pre_b7_revenue_day=pre_b7_revenue_today
gen pre_b8_profit_day=pre_b8_profit_today
gen pre_c5_farm_dum=pre_farm_dum
gen pre_c5_farm_hrs_week=pre_c5_farm_hrs
gen pre_b1_work_=pre_b1_work_prob
gen pre_b2_hours_r = pre_b1_work_hrs
		
*HFPS variables
egen hfps_b6_inv_equip=rowtotal(hfps_b6_inventory_week hfps_a3_equip_exp ), missing/*weekly inventory+equipment investment*/
egen pre_b6_inv_equip=rowtotal(pre_b6_inventory_today pre_a3_equip_exp), missing
gen pre_b6_inventory_equip=pre_b6_inv_equip
*total earnings
egen hfps_all_income_week=rowtotal(hfps_a4_b8_income_week hfps_b8_profit_week), missing 
lab var hfps_all_income_week "Total weekly income: main business, 2nd activity + Salary"
*hours
bysort id hfps_round hfps_week_ind: egen hfps_b1_main_hrs_week=total(hfps_b1_work_hrs), missing /*generating total hours worked in the main occupation at the weekly level, these are derived from the daily values*/
egen hfps_all_hrs_week=rowtotal(hfps_b1_work_hrs hfps_a4_b8_hrs_week), missing
lab var hfps_all_hrs_week "Total weekly hrs: main business, 2nd activity + Salary"

	
*Monitoring survey variables
gen ms_b7_revenue_day=ms_b7_revenue_week/7
gen ms_b8_profit_day=ms_b8_profit_week/7
egen ms_a7_equipment_invest_1m=rowtotal(ms_a7_equipment_invest_dec ms_a7_equipment_invest_mar), missing
gen ms_a7_equipment_invest_1w=ms_a7_equipment_invest_1m/4
egen ms_b6_inventory_equip=rowtotal(ms_a7_equipment_invest_1w ms_b6_inventory), missing
	
gen ms_a4_2nd_oc_sal_week=ms_a4_2nd_oc_sal_mar/4 /*weekly salaried income*/
replace ms_a4_2nd_oc_sal_week=ms_a4_2nd_oc_sal_dec/4 if ms_a4_2nd_oc_sal_week==. /*weekly salaried income*/
	 
egen ms_a4_b8_hrs_week=rowtotal(ms_c5_o_hrs_week ms_a4_2nd_oc_hrs_sal_week), missing/*VB: note that c5_o_hrs_week (hours in non-salaried OC) was not collected at MS1*/
egen ms_a4_b8_income_week=rowtotal(ms_a4_2nd_oc_sal_week  ms_c5_o_earn_week), missing /*VB: note that c5_o_earn_week (hours in non-salaried OC) was not collected at MS1*/
gen ms_a4_b8_dum_week=(ms_a4_b8_hrs_week>0) if !missing(ms_a4_b8_hrs_week)

egen ms_credit_customers=rowtotal(ms_tr_give_store_cr_mar ms_tr_give_store_cr_jan ms_tr_give_store_cr_dec ms_tr_give_store_cr_nov), missing
 
*****************************************************************************
************************EXPENDITURES****************************************** 
*****************************************************************************
******************
*Generate dependent variables, and pre-treatment controls from the intake survey, for ANCOVA analysis
******************
drop hfps_exp_tot_week
*replace ms_exp1=ms_exp1/700
		
*Food
gen pre_exp1=pre_exp1_week
		
*Personal expenditures
egen hfps_exp_per_week=rowtotal(hfps_exp2_week hfps_exp9_week  hfps_exp7_week), missing
lab var hfps_exp_per_week "Aggregate  Weekly: Restaurant + Clothes + Entertainment"
egen ms_exp_per_week=rowtotal(ms_exp2 ms_exp11  ms_exp7), missing
lab var ms_exp_per_week "Aggregate  Weekly: Restaurant + Clothes + Entertainment"
egen pre_exp_per_week=rowtotal(pre_exp2_today), missing
lab var pre_exp_per_week "Aggregate  Weekly: Personal"
		 
*Household expenditures
egen hfps_exp_hh_week=rowtotal(hfps_exp3_week hfps_exp4_week ), missing
lab var hfps_exp_hh_week "Aggregate  Weekly: HH items + HH bills "
egen ms_exp_hh_week=rowtotal(ms_exp3 ms_exp4 ), missing
lab var ms_exp_hh_week "Aggregate  Weekly: HH items + HH bills "
egen pre_exp_hh_week=rowtotal(pre_exp3_today pre_exp4_today), missing
lab var pre_exp_hh_week "Aggregate  Weekly: HH items + HH bills "
		
*Medical expenditures
egen hfps_exp_med_week=rowtotal(hfps_exp8_week hfps_exp14a_week hfps_exp14b_week), missing	
lab var hfps_exp_med_week "Aggregate  weekly: Med Expenses"
egen ms_exp_med_week=rowtotal(ms_exp8 ms_exp10 ms_exp9), missing
lab var ms_exp_med_week "Aggregate  weekly: Med Expenses"
egen pre_exp_med_week=rowtotal(pre_hh_sick_self_cost pre_hh_sick_member_cost), missing
lab var pre_exp_med_week "Aggregate  Weekly: Medical"
		 
*Other expenditures
egen hfps_exp_other_week= rowtotal( hfps_exp16_week hfps_exp11_week hfps_exp6_week hfps_exp12_week hfps_exp13_week hfps_exp15_week hfps_exp17_week hfps_exp_med_week), missing
lab var hfps_exp_other_week "Aggregate  Weekly: Other (births/weddings, donations, transport, durables, funerals, other unhfps_expected + medical"
egen ms_exp_other_week= rowtotal( ms_exp11 ms_exp6 ms_exp_med_week), missing /*exp16 is missing at MS*/
lab var ms_exp_other_week "Aggregate  Weekly: Other (births/weddings, donations, transport, durables, funerals, other unms_expected + medical"
egen pre_exp_other_week= rowtotal(pre_hh_funeral_exp pre_hh_other_exp pre_exp4_today), missing
lab var pre_exp_other_week "Aggregate  Weekly: Other (births/weddings/funerals, other unpre_expected"
		 
*Education expenditures
gen hfps_exp_educ_week=hfps_exp10_week
egen ms_exp_educ=rowtotal(ms_ed_fees_am ms_ed_exp_o_am), missing /*generating total expenses for education*/
replace ms_exp_educ=0 if ms_round!=. & ms_ed_fees_am==.

*converting to weekly equivalents
gen ms_exp_educ_daily=ms_exp_educ
replace ms_exp_educ_daily=ms_exp_educ_daily/(3*4) if ms_round==2 /*measured over the last 3 months*/
replace ms_exp_educ_daily=ms_exp_educ_daily/(6*4) if ms_round==1 /*measured over the last 6 months*/

gen ms_holiday_spending_tot_w=ms_holiday_spending_tot/(3*4) if ms_round==2 /*approximately Jan,Feb,Mar*/
replace ms_holiday_spending_tot_w=ms_holiday_spending_tot/(6*4) if ms_round==1 /*Jul-Jan*/


*Total expenditures
egen hfps_exp_tot_week=rowtotal( ///
	hfps_exp1_week hfps_exp2_week hfps_exp3_week hfps_exp4_week hfps_exp5_week hfps_exp6_week hfps_exp7_week hfps_exp8_week hfps_exp9_week 	///
	hfps_exp10_week hfps_exp11_week hfps_exp12_week hfps_exp13_week hfps_exp14a_week hfps_exp14b_week hfps_exp15_week hfps_exp16_week ///
	hfps_exp17_week), missing
lab var hfps_exp_tot_week "Aggregate weekly: Total"

replace ms_exp_total=ms_exp_total+ms_exp_educ_daily

*generating variables that are not present in the pre-treatment survey XX (why don't we have some version of a total?) XX
foreach var in exp5_week exp5 exp_tot_week exp_educ_week exp_total ms_holiday_spending_tot ///
	land_purchase land_rent farm_inputs assets_dum assets_kw {
	gen pre_`var'=0
	}
	

	
*****************************************************************************
************************TRANSFERS******************************************** 
*****************************************************************************
******************
*Generate dependent variables, and pre-treatment controls from the intake survey, for ANCOVA analysis
******************
gen pre_cr_loan_dig_tot_am=0

*XX - move to createvars XX
drop  hfps_tr_receive_non_sp_total hfps_tr_all_received_dum hfps_tr_all_received_am hfps_tr_give_sp_dum ///
 hfps_tr_give_non_sp_total hfps_tr_all_given_dum hfps_tr_all_given_am hfps_tr_give_non_sp_total hfps_tr_all_given_dum ///
 hfps_tr_all_given_am  hfps_tr_receive_sp_dum hfps_cr_loan_dig_dum hfps_cr_loan_formal_dum hfps_cr_loan_formal_am ms_tr_receive_sp_dum ms_tr_give_sp_am ms_tr_give_sp_dum ///
		
*Generate HFPS variables

*receiving transfers (not from spouse)	
egen hfps_tr_receive_non_sp_total=rowtotal(hfps_tr_receive_am_cash hfps_tr_receive_am_inkind hfps_tr_receive_am_services hfps_tr_receive_am_mm hfps_tr_receive_am_zoona), ///
 missing /*excludes airtime*/
lab var hfps_tr_receive_non_sp_total "Total Transfers Received, Excluding Spouse"

egen hfps_tr_all_received_am=rowtotal(hfps_tr_receive_non_sp_total hfps_cr_loan_6_am hfps_cr_loan_7_am hfps_cr_loan_8_a), missing
lab var hfps_tr_all_received_am "Last week Gifts/loans from Friends/Relatives/Shopkeeper and Other informal: Amount"
				
egen ms_tr_all_received_am=rowtotal(ms_tr_receive_mar ms_tr_receive_loan_mar  ms_tr_receive_dec ms_tr_receive_loan_dec) if ms_round!=. 
lab var ms_tr_all_received_am "Last Month Gifts/loans from Friends/Relatives/Shopkeeper and Other informal: Amount"
				
gen hfps_tr_all_received_dum=0 if hfps_cr_loan_dum!=.
replace hfps_tr_all_received_dum=1 if hfps_cr_loan_6_dum==1 | hfps_cr_loan_7_dum==1 | hfps_cr_loan_8_dum==1 /*loans*/
replace hfps_tr_all_received_dum=1 if hfps_tr_receive_non_sp_total>0 & !missing(hfps_tr_receive_non_sp_total) /*non-spousal transfers + gifts*/
lab var hfps_tr_all_received_dum "Last wekReceived Gifts/loans from Friends/Relatives/Shopkeeper and Other informal:Binary"
				
gen ms_tr_all_received_dum=(ms_tr_all_received_am>0) if ms_round!=.
lab var ms_tr_all_received_dum "Last month Received Gifts/loans from Friends/Relatives/Shopkeeper and Other informal:Binary"
				
*receiving transfers (spouse)
replace hfps_tr_receive_sp_am=0 if hfps_tr_receive_sp_am==. & hfps_r_day_long==1/*making unconditional on being married */
gen hfps_tr_receive_sp_dum=(hfps_tr_receive_sp_am>0) if hfps_r_day_long==1
lab var hfps_tr_receive_sp_dum "Transfers Received from Spouse: Binary"

*receiving transfers (spouse + non-spouse)
egen hfps_tr_receive_sp_nonsp_am=rowtotal(hfps_tr_receive_sp_am hfps_tr_all_received_am), missing
lab var hfps_tr_receive_sp_nonsp_am "Total transfers received: spouse + non-spouse"

	gen hfps_tr_receive_sp_nonsp_dum=(hfps_tr_receive_sp_nonsp_am>0)
	replace hfps_tr_receive_sp_nonsp_dum=. if missing(hfps_tr_receive_sp_nonsp_am)
	lab var hfps_tr_receive_sp_nonsp_dum "Total transfers received - binary: spouse + non-spouse" 
				
egen ms_tr_receive_sp_am=rowtotal(ms_tr_receive_sp_mar ms_tr_receive_sp_dec) if ms_round!=. /*over the past month*/
lab var ms_tr_receive_sp_am "Last month Transfers Received from Spouse: Amount"

gen ms_tr_receive_sp_dum=(ms_tr_receive_sp_am>0) if ms_round!=.
lab var ms_tr_receive_sp_dum "Transfers Received from Spouse: Binary"

	egen ms_tr_receive_sp_nonsp_am=rowtotal(ms_tr_receive_sp_am ms_tr_all_received_am)
	lab var  ms_tr_receive_sp_nonsp_am "Total transfers received: spouse + non-spouse"
	
	gen ms_tr_receive_sp_nonsp_dum=(ms_tr_receive_sp_nonsp_am>0)
	replace ms_tr_receive_sp_nonsp_dum=. if missing(ms_tr_receive_sp_nonsp_dum)
	lab var ms_tr_receive_sp_nonsp_dum "Total transfers received - binary: spouse + non-spouse"
			
	
*giving transfers (not from spouse)
egen hfps_tr_given_non_sp_total=rowtotal(hfps_tr_give_am_cash hfps_tr_give_am_inkind hfps_tr_give_am_services hfps_tr_give_am_mm hfps_tr_give_am_zoona) /*excludes airtime*/
replace hfps_tr_given_non_sp_total=. if hfps_r_day_long!=1
lab var hfps_tr_given_non_sp_total "Total Transfers Given, Excluding Spouse"
				
gen hfps_tr_all_given_dum=0 if hfps_cr_loan_dum!=.
replace hfps_tr_all_given_dum=1 if hfps_tr_give_loan_num>0 & !missing(hfps_tr_give_loan_num)
replace hfps_tr_all_given_dum=1 if 	hfps_tr_given_non_sp_total>0 & !missing(hfps_tr_given_non_sp_total)
lab var hfps_tr_all_given_dum "Given Gifts/loans to Friends/Relatives/Shopkeeper and Other informal:Binary"
				
egen hfps_tr_all_given_am=rowtotal(hfps_tr_given_non_sp_total hfps_tr_give_loan_am), missing
lab var hfps_tr_all_given_am "Given Gifts/loans from Friends/Relatives/Shopkeeper and Other informal: Amount"

egen hfps_tr_given_sp_nonps_am=rowtotal(hfps_tr_all_given_am hfps_tr_give_sp_am), missing
lab var hfps_tr_given_sp_nonps_am "Total transfers given: spouse + non-spouse"
	

	gen  hfps_tr_given_sp_nonps_dum=( hfps_tr_given_sp_nonps_am>0)
	replace  hfps_tr_given_sp_nonps_dum=. if missing( hfps_tr_given_sp_nonps_am)
	lab var hfps_tr_given_sp_nonps_dum "Total transfers given - binary: spouse + non-spouse"
			
egen ms_tr_all_given_am=rowtotal(ms_tr_give_dec ms_tr_give_mar), missing
lab var ms_tr_all_given_am "Last month Given Gifts/loans from Friends/Relatives/Shopkeeper and Other informal: Amount"
				
gen ms_tr_all_given_dum=(ms_tr_all_given_am>0) if !missing(ms_tr_all_given_am)
lab var ms_tr_all_given_dum "Last month Given Gifts/loans from Friends/Relatives/Shopkeeper and Other informal: Binary"



*giving transfers (spouse)
replace hfps_tr_give_sp_am=0 if hfps_tr_give_sp_am==. & hfps_r_day_long==1  /*making unconditional on being married */
gen hfps_tr_give_sp_dum=(hfps_tr_give_sp_am>0) if !missing(hfps_tr_give_sp_am)
lab var hfps_tr_give_sp_dum "Transfers Given Spouse: Binary"
				
egen ms_tr_give_sp_am=rowtotal(ms_tr_give_sp_mar ms_tr_give_sp_dec), missing
lab var ms_tr_give_sp_am "Last month Given to spouse last Month"			

	*Fix an error - some data issue with transfers given vs. received
	replace ms_tr_give_sp_am=0 if ms_tr_receive_sp_am==0 & ms_tr_give_sp_am ==.

	gen ms_tr_give_sp_dum=(ms_tr_give_sp_am>0) if !missing(ms_tr_give_sp_am)
	lab var ms_tr_give_sp_dum "Last month Transfers Given Spouse: Binary"	

	*giving transfers (spouse + non-spouse)
	egen ms_tr_given_sp_nonps_am=rowtotal(ms_tr_all_given_am ms_tr_give_sp_am), missing
	lab var ms_tr_given_sp_nonps_am "Total transfers given: spouse + non-spouse"	
	
		gen ms_tr_given_sp_nonps_dum=(ms_tr_given_sp_nonps_am>0)
		replace ms_tr_given_sp_nonps_dum=. if missing(ms_tr_given_sp_nonps_am)
		lab var ms_tr_given_sp_nonps_dum "Total transfers given - binary: spouse + non-spouse"

*pre vars
gen pre_tr_receive_sp_nonsp_dum=pre_tr_receive_total_dum
gen pre_tr_given_sp_nonps_dum=pre_tr_give_total_dum
	
*digital credit - Kutchova, Kutapa + Pasavute
gen hfps_cr_loan_dig_dum=(hfps_cr_loan_dig_num>0) if !missing(hfps_cr_loan_dig_num)
lab var hfps_cr_loan_dig_dum "Took out a digital loan Kutchova, Kutapa, Pasavute: Binary"
				
egen hfps_cr_loan_dig_tot_am=rowtotal(hfps_cr_loan_dig_am*), missing
lab var hfps_cr_loan_dig_tot_am "DIgital Loan Kutchova, Kutapa, Pasavute: Amount"
				
/*kutchova amounts are cumulative for 6 months for MS1 and 3 months for MS2 but the amounts are small, so it won't make a difference*/
replace ms_kutchova_loan_am=ms_kutchova_loan_am/6 if ms_round==1 /*converting the cumulative value into per month terms*/
replace ms_kutchova_loan_am=ms_kutchova_loan_am/3 if ms_round==2/*converting the cumulative value into per month terms*/
				
egen ms_cr_loan_dig_tot_am=rowtotal(ms_cr_loan_kut_pasa_am_lm ms_kutchova_loan_am) if ms_round!=. 
lab var ms_cr_loan_dig_tot_am "Digital Loan Kutchova, Kutapa, Pasavute: Amount"
				
gen ms_cr_loan_dig_dum=(ms_cr_loan_dig_tot_am>0) if ms_round!=.
lab var ms_cr_loan_dig_dum "Took out a digital loan Kutchova, Kutapa, Pasavute: Binary"
		
*all other non-digital loans
gen hfps_cr_loan_formal_dum=(hfps_cr_loan_1_dum==1 | hfps_cr_loan_2_dum==1 | hfps_cr_loan_3_dum==1 | hfps_cr_loan_4_dum==1 | hfps_cr_loan_5_dum==1 | hfps_cr_loan_6_dum==1 | hfps_cr_loan_10_dum==1) if !missing(hfps_cr_loan_dum)     
lab var hfps_cr_loan_formal_dum "Took out a loan from ROSCA, MFI, Bank, ML, VSLA, Store Credit"
				
egen hfps_cr_loan_formal_am=rowtotal(hfps_cr_loan_1_am hfps_cr_loan_2_am hfps_cr_loan_3_am hfps_cr_loan_4_am hfps_cr_loan_5_am hfps_cr_loan_6_am hfps_cr_loan_10_am) if !missing(hfps_cr_loan_dum)
lab var hfps_cr_loan_formal_am "Total Loan Amount from ROSCA, MFI, Bank, ML, VSLA, Store Credit"
				
egen ms_cr_loan_formal_am=rowtotal(ms_cr_loan_vsla_am_total ms_cr_loan_mfi_am_total ms_cr_loan_ml_am_total ms_cr_loan_rosca_am_total ms_cr_loan_bank_am_total) if ms_round!=.
replace ms_cr_loan_formal_am=ms_cr_loan_formal_am/6 if ms_round==1 /*converting the cumulative value into per month terms*/
replace ms_cr_loan_formal_am=ms_cr_loan_formal_am/3 if ms_round==2 /*converting the cumulative value into per month terms*/
				
gen ms_cr_loan_formal_dum=(ms_cr_loan_formal_am>0) if ms_round!=. /*JON, this variable is not correct and does not represent probability of taking a loan in a given month*/

*Total credit				
gen hfps_cr_loan_all_dum=(hfps_cr_loan_formal_dum==1 | hfps_cr_loan_dig_dum>0) if !missing(hfps_cr_loan_formal_dum)
lab var hfps_cr_loan_dig_dum "Took out a digital loan or another loan: Binary"
egen hfps_cr_loan_all_am=rowtotal(hfps_cr_loan_dig_tot_am hfps_cr_loan_formal_am), missing
lab var hfps_cr_loan_all_am "Took out a digital loan or another loan: Amount"

*Monitoring surveys				
gen ms_cr_loan_all_dum=(ms_cr_loan_formal_dum==1 | ms_cr_loan_dig_dum>0) if !missing(ms_cr_loan_formal_dum)
lab var ms_cr_loan_dig_dum "Took out a digital loan or another loan: Binary"
egen ms_cr_loan_all_am=rowtotal(ms_cr_loan_dig_tot_am ms_cr_loan_formal_am), missing
lab var ms_cr_loan_all_am "Took out a digital loan or another loan: Amount"

egen pre_cr_loan_all_am=rowtotal(pre_cr_loan_dig_tot_am pre_cr_loan_formal_am), missing
gen pre_cr_loan_all_dum=(pre_cr_loan_all_am>0) if pre_cr_loan_all_am!=.

*****Time preferences - CHECK*
numlabel, add 
rename bl_tp1 bl_tp_now_2w_500_r0
rename bl_tp2 bl_tp_4hrs_2w_500_r0
rename bl_tp3 bl_tp_now_2w_5000_r0
rename bl_tp4 bl_tp_now_2w_5000_r10
rename bl_tp5 bl_tp_now_2w_5000_r25
rename bl_tp6 bl_tp_now_2w_5000_r50
rename bl_tp7 bl_tp_now_2w_5000_r100
rename bl_tp8 bl_tp_now_2w_5000_r200

rename bl_tp9  bl_tp_2w_4w_500_r0
rename bl_tp10 bl_tp_2w_4w_5000_r0
rename bl_tp11 bl_tp_2w_4w_5000_r10
rename bl_tp12 bl_tp_2w_4w_5000_r25
rename bl_tp13 bl_tp_2w_4w_5000_r50
rename bl_tp14 bl_tp_2w_4w_5000_r100
rename bl_tp15 bl_tp_2w_4w_5000_r200

foreach var of varlist bl_tp*{
gen `var'_sooner=(`var'==1) if !missing(`var')
}


gen bl_tp_inconsistent=0 if bl_tp_now_2w_5000_r0_sooner!=.

foreach r of num 0 10 25 50 100 200{
	gen bl_tp_inconsistent_r`r'=(bl_tp_now_2w_5000_r`r'_sooner==1 & bl_tp_2w_4w_5000_r`r'_sooner==0) /*takes now BUT can wait for later payment in 2 weeks*/
	replace bl_tp_inconsistent_r`r'=. if bl_tp_now_2w_5000_r`r'_sooner==.
	*gen bl_tp_consistent_r`r'=(bl_tp_now_2w_5000_r`r'_sooner==bl_tp_2w_4w_5000_r`r'_sooner) 
	replace bl_tp_inconsistent=1 if bl_tp_inconsistent_r`r'==1
}


***************
*Record Keeping
***************

gen ms_hh_satisfied_yes=0 if ms_hh_satisfied==3
replace ms_hh_satisfied_yes=1 if ms_hh_satisfied==1 | ms_hh_satisfied==2

replace ms_cm_bus_personal_times=ms_cm_bus_personal_times*700 /*it was accidentally converted to USD*/
	
***************
*Risk Coping
***************	
	
foreach self in self member{
foreach box in box mm{
foreach one in one multi{
gen sick_`self'X`one'_`box'=hfps_hh_sick_`self'_dum*`one'_`box'
}
}
}


foreach self in member self{
gen hfps_sick_`self'Xmm=hfps_hh_sick_`self'_dum*mobile_money
}
	
********************************************
*Lottery Payments & heterogeneity analysis
*******************************************

*lottery payments period before
bysort id (hfps_actual_date): gen hfps_lottery_ysday_dum=hfps_lottery_day_dum[_n-1]
lab var hfps_lottery_ysday_dum "Won Lottery Yesterday: Binary"

preserve
*lottery payments in the week before
*#delimit
keep hfps_lottery_week_dum hfps_week_ind id
duplicates drop
drop if hfps_week_ind==.
bysort id (hfps_week_ind): gen hfps_lottery_lsweek_dum=hfps_lottery_week_dum[_n-1]
drop hfps_lottery_week_dum
tempfile lottery
save `lottery'
restore

merge m:1 id hfps_week_ind using `lottery', gen(_m1)
drop _m1

gen any_account=0 if control==1
replace any_account=1 if mobile_money==1 | one_box==1 | multi_box==1

gen bl_has_mobile_money=1 if bl_mm4_use==1
replace bl_has_mobile_money=0 if bl_mm1_heard==0
replace bl_has_mobile_money=0 if bl_mm2_==0
replace bl_has_mobile_money=0 if bl_mm4_use==2 | bl_mm4_use==3

gen bl_mm=bl_has_mobile_money
gen bl_box=bl_sav10_box_dum
foreach g in one_box multi_box one_mm multi_mm any_account {
gen hfps_lottery_dayX`g'=`g'*hfps_lottery_ysday_dum /*for daily regressions*/
gen hfps_lottery_weekX`g'=`g'*hfps_lottery_lsweek_dum /*for weekly regressions*/
gen bl_femaleX`g'=bl_female*`g' /*gender*/
gen bl_taxedX`g'=bl_taxed*`g' /*taxed*/
gen bl_tp_inconsistentX`g'=bl_tp_inconsistent*`g' /*TP inconsistent*/
gen bl_mmX`g'=bl_mm*`g' /*has mm account*/
gen bl_boxX`g'=bl_box*`g' /*has box*/
}


******************
*Winsorize all variables
******************

gen pre_b8_profit_week=pre_b8_profit_today*7 /*approximation of weekly profits in pre-survey*/
gen pre_b7_revenue_week=pre_b7_revenue_day*7 /*approximation of weekly profits in pre-survey*/
gen pre_all_income_week=0 /*don't have info on totals, but these vars aren't need coping with shocks since they are FE at the individual level, only added to make the winsorizing code run*/
gen pre_all_hrs_week=0 /*don't have info on totals, but these vars aren't need coping with shocks since they are FE at the individual level, only added to make the winsorizing code run*/
gen pre_tr_receive_am_mm=0 /*don't have info on MM transfers in PRE*/
gen pre_tr_give_am_mm=0 /*don't have info on MM transfers in PRE*/
rename pre_tr_receive_total  pre_tr_receive_sp_nonsp_am
rename pre_tr_give_total 	pre_tr_given_sp_nonps_am
gen pre_exp10_week=pre_exp_educ_week

*gen pre_credit_customers=pre_tr_give_loan_am
gen pre_credit_customers=0

*HFPS & pre-treatment variables
foreach var in ///
 d_dep_box_IPA_tot_week d_dep_nonIPA_box_week d_dep_home_week d_dep_bank_week d_dep_svgroup_tot_week d_dep_nonIPA_mm_week d_dep_mm_IPA_tot_week d_dep_other_week d_dep_TOTAL_week ///
 d_wit_box_IPA_tot_week d_wit_nonIPA_box_week d_wit_home_week d_wit_bank_week d_wit_svgroup_tot_week d_wit_nonIPA_mm_week d_wit_mm_IPA_tot_week d_wit_other_week d_wit_TOTAL_week ///
 d_nd_box_IPA_tot_week d_nd_nonIPA_box_week d_nd_home_week d_nd_bank_week d_nd_svgroup_tot_week d_nd_nonIPA_mm_week d_nd_mm_IPA_tot_week d_nd_other_week d_nd_TOTAL_week /// 
 d_dep_mmboxIPA_tot_week 		d_wit_mmboxIPA_tot_week 		///
 d_dep_box_ALL_tot_week 		d_wit_box_ALL_tot_week	///
 d_dep_mm_ALL_tot_week 			d_wit_mm_ALL_tot_week ///
 d_nd_mmboxIPA_tot_week			d_nd_mm_ALL_tot_week	///
 d_nd_box_ALL_tot_week /// 
 b1_work_hrs b7_revenue_today b8_profit_today b6_inv_equip c5_farm_hrs a4_b8_hrs_week a4_b8_income_week ///
 exp1_week exp_per_week exp5_week exp_hh_week exp10_week exp_other_week exp_tot_week exp_med_week exp_educ_week ///
 tr_all_received_am tr_all_given_am tr_receive_sp_am tr_give_sp_am cr_loan_all_am ///
 b8_profit_week b7_revenue_week all_income_week all_hrs_week ///
tr_receive_am_mm tr_give_am_mm ///
tr_receive_sp_nonsp_am  tr_given_sp_nonps_am {

	gen hfps_`var'_`win'=hfps_`var'
	qui sum hfps_`var' if hfps_`var'!=0, d
	replace hfps_`var'_`win'=r(p`100minuswin') if hfps_`var'>r(p`100minuswin') & !missing(hfps_`var')
							
	gen pre_`var'_`win'=pre_`var'
	qui sum pre_`var' if pre_`var'!=0, d
	replace pre_`var'_`win'=r(p`100minuswin') if pre_`var'>r(p`100minuswin') & !missing(pre_`var')
	}
	
	
*Monitoring surveys
foreach var in 	///
 d_dep_box_ipa_2m_total d_dep_box_2m_total d_dep_home_2m_total d_dep_bank_2m_total d_dep_svgroup_tot_2m d_dep_mm_2m_total d_dep_othersp_2m_total   ///
 d_wit_box_ipa_2m_total d_wit_box_2m_total d_wit_home_2m_total d_wit_bank_2m_total d_wit_mm_2m_total d_wit_othersp_2m_total  ///
 d_bal_box_IPA_tot d_bal_box d_bal_home d_bal_bank d_bal_svgroup d_bal_mm d_bal_othersp ///
 d_dep_mm_ipa_total d_wit_mm_ipa_total d_bal_mm_IPA_tot ///
 d_nd_box_ipa_2m_total d_nd_box_2m_total d_nd_home_2m_total d_nd_bank_2m_total d_nd_mm_2m_total d_nd_othersp_2m_total d_nd_mm_ipa_total ///
 d_bal_mmboxIPA_tot 	 d_bal_mm_ALL_tot d_bal_box_ALL_tot ///
 d_dep_mmboxIPA_2m_tot d_dep_mm_ALL_2m_tot d_dep_box_ALL_2m_tot ///
 d_wit_mmboxIPA_2m_tot d_wit_mm_ALL_2m_tot d_wit_box_ALL_2m_tot ///
 d_nd_mmboxIPA_2m_tot d_nd_mm_ALL_2m_tot d_nd_box_ALL_2m_tot /// 
 b2_work_hrs_day b7_revenue_day b8_profit_day b6_inventory_equip c5_farm_hrs_week a4_b8_hrs_week a4_b8_income_week credit_customers farm_inputs ///
 exp1 exp_per_week exp5 exp_hh_week exp_educ exp_other_week exp_total  assets_kw exp_educ_daily  holiday_spending_tot_w ///
 tr_all_received_am tr_all_given_am tr_receive_sp_am tr_give_sp_am cr_loan_all_am ///
 reg_pur_kw em_amount em_hungry_season_amount cm_bus_personal_times ///
 ed_reduce_am hh_shock_o_am ///
 tr_receive_sp_nonsp_am tr_given_sp_nonps_am {

	gen ms_`var'_`win'=ms_`var'
	qui sum ms_`var' if ms_`var'!=0, d
	replace ms_`var'_`win'=r(p`100minuswin') if ms_`var'>r(p`100minuswin') & !missing(ms_`var')
	
	capture gen pre_`var'=0
	capture gen pre_`var'_`win'=pre_`var'
	qui sum pre_`var' if pre_`var'!=0, d
	replace pre_`var'_`win'=r(p`100minuswin') if pre_`var'>r(p`100minuswin') & !missing(pre_`var')	
	}

*FOR SAVINGS: Take the sum of the winsorized amounts, rather than winsorize the sum
	drop hfps_d_dep_TOTAL_week_`win' hfps_d_wit_TOTAL_week_`win' hfps_d_nd_TOTAL_week_`win'

	egen hfps_d_dep_TOTAL_week_`win'=rowtotal(hfps_d_dep_home_week_`win' hfps_d_dep_box_IPA_tot_week_`win' hfps_d_dep_nonIPA_box_week_`win' ///
	 hfps_d_dep_bank_week_`win' hfps_d_dep_svgroup_tot_week_`win' hfps_d_dep_nonIPA_mm_week_`win' hfps_d_dep_other_week_`win' hfps_d_dep_mm_IPA_tot_week_`win'), missing
	egen hfps_d_wit_TOTAL_week_`win'=rowtotal(hfps_d_wit_home_week_`win' hfps_d_wit_box_IPA_tot_week_`win' hfps_d_wit_nonIPA_box_week_`win' ///
	 hfps_d_wit_bank_week_`win' hfps_d_wit_svgroup_tot_week_`win' hfps_d_wit_nonIPA_mm_week_`win' hfps_d_wit_other_week_`win' hfps_d_wit_mm_IPA_tot_week_`win'), missing
	egen hfps_d_nd_TOTAL_week_`win'=rowtotal(hfps_d_nd_home_week_`win' hfps_d_nd_box_IPA_tot_week_`win' hfps_d_nd_nonIPA_box_week_`win' ///
	 hfps_d_nd_bank_week_`win' hfps_d_nd_svgroup_tot_week_`win' hfps_d_nd_nonIPA_mm_week_`win' hfps_d_nd_other_week_`win' hfps_d_nd_mm_IPA_tot_week_`win'), missing

	
*FOR SAVINGS: Take the sum of the winsorized amounts, rather than winsorize the sum
	egen ms_d_dep_TOTAL_`win'=rowtotal(ms_d_dep_box_ipa_2m_total_`win' ms_d_dep_box_2m_total_`win' ms_d_dep_home_2m_total_`win' ms_d_dep_bank_2m_total_`win' ///
	 ms_d_dep_svgroup_tot_2m_`win' ms_d_dep_mm_2m_total_`win' ms_d_dep_othersp_2m_total_`win' ms_d_dep_mm_ipa_total_`win'), missing
	egen ms_d_wit_TOTAL_`win'=rowtotal(ms_d_wit_box_ipa_2m_total_`win' ms_d_wit_box_2m_total_`win' ms_d_wit_home_2m_total_`win' ms_d_wit_bank_2m_total_`win' ///
								   ms_d_wit_mm_2m_total_`win' ms_d_wit_othersp_2m_total_`win' ms_d_wit_mm_ipa_total_`win'), missing 
	egen ms_d_nd_TOTAL_`win'=rowtotal(ms_d_nd_box_ipa_2m_total_`win' ms_d_nd_box_2m_total_`win' ms_d_nd_home_2m_total_`win' ms_d_nd_bank_2m_total_`win' ///
								   ms_d_nd_mm_2m_total_`win' ms_d_nd_othersp_2m_total_`win' ms_d_nd_mm_ipa_total_`win'), missing 
	egen ms_d_bal_TOTAL_`win'=rowtotal(ms_d_bal_box_IPA_tot_`win' ms_d_bal_box_`win' ms_d_bal_home_`win' ms_d_bal_bank_`win' ///
	 ms_d_bal_svgroup_`win' ms_d_bal_mm_`win' ms_d_bal_othersp_`win' ms_d_bal_mm_IPA_tot_`win'), missing
	 
	 gen pre_d_dep_TOTAL_`win'=pre_d_dep_TOTAL_week_`win'
	 gen pre_d_wit_TOTAL_`win'=pre_d_wit_TOTAL_week_`win'

	*total MM savings
	egen hfps_d_dep_mm_TOT_week_`win'=rsum(hfps_d_dep_mm_IPA_tot_week_`win' hfps_d_dep_nonIPA_mm_week_`win'), missing
	egen hfps_d_wit_mm_TOT_week_`win'=rsum(hfps_d_wit_mm_IPA_tot_week_`win' hfps_d_wit_nonIPA_mm_week_`win'), missing
	egen hfps_d_nd_mm_TOT_week_`win'=rsum(hfps_d_nd_mm_IPA_tot_week_`win' hfps_d_nd_nonIPA_mm_week_`win'), missing

	gen hfps_D_use_mm_week=1 if hfps_d_dep_mm_TOT_week_`win'>0 & hfps_d_dep_mm_TOT_week_`win'!=.
	replace hfps_D_use_mm_week=0 if hfps_d_dep_mm_TOT_week_`win'==0 


	egen ms_d_dep_mm_TOT_`win'=rsum(ms_d_dep_mm_ipa_total_`win' ms_d_dep_mm_2m_total_`win'), missing
	egen ms_d_wit_mm_TOT_`win'=rsum(ms_d_wit_mm_ipa_total_`win' ms_d_wit_mm_2m_total_`win'), missing
	egen ms_d_nd_mm_TOT_`win'=rsum(ms_d_nd_mm_ipa_total_`win' ms_d_nd_mm_2m_total_`win'), missing
	
	gen pre_d_nd_TOTAL_`win'=pre_d_nd_TOTAL_week_`win'

	gen ms_D_use_mm=1 if ms_d_dep_mm_TOT_`win'>0 & ms_d_dep_mm_TOT_`win'!=.
	replace ms_D_use_mm=0 if ms_d_dep_mm_TOT_`win'==0

	egen ms_d_bal_mm_TOT_`win'=rsum(ms_d_bal_mm_IPA_tot_`win' ms_d_bal_mm_`win'), missing

	*XX to fix - fill in the pre values of total MM deposits / withdrawals
	gen pre_d_dep_mm_TOT_week_w5=0
	gen pre_d_wit_mm_TOT_week_w5=0
	gen pre_d_nd_mm_TOT_week_w5=0
	gen pre_D_use_mm_week=0


egen rowobs=rownonmiss(hfps_d_dep_box_IPA_tot_week_`win' hfps_d_dep_nonIPA_box_week_`win' hfps_d_dep_home_week_`win' hfps_d_dep_bank_week_`win' hfps_d_dep_svgroup_tot_week_`win' ///
 hfps_d_dep_nonIPA_mm_week_`win' hfps_d_dep_other_week_`win' hfps_d_dep_TOTAL_week_`win' /// 
 hfps_d_wit_box_IPA_tot_week_`win' hfps_d_wit_nonIPA_box_week_`win' hfps_d_wit_home_week_`win' hfps_d_wit_bank_week_`win' hfps_d_wit_svgroup_tot_week_`win' ///
 hfps_d_wit_nonIPA_mm_week_`win' hfps_d_wit_other_week_`win' hfps_d_wit_TOTAL_week_`win')
 
gen hfps_in_regress=1 if rowobs==16

egen pre_d_bal_TOTAL_`win' =rowtotal(pre_d_bal_bank_`win' pre_d_bal_box_ALL_tot_`win' pre_d_bal_home_`win' pre_d_bal_mm_ALL_tot_`win' pre_d_bal_othersp_`win' pre_d_bal_svgroup_`win'), missing

*FOR BUSINESS:

	gen  ms_credit_customers_w5_d=ms_credit_customers_w5/30
	gen  pre_credit_customers_w5_d=pre_tr_give_loan_am
	
*FOR EXPENDITURES
egen hfps_exp_hhsum_week_`win'=rsum(hfps_exp1_week_`win' hfps_exp_hh_week_`win'), missing
egen pre_exp_hhsum_week_`win'=rsum(pre_exp1_week_`win' pre_exp_hh_week_`win'), missing	

egen ms_exp_hhsum_week_`win'=rsum(ms_exp1_`win' ms_exp_hh_week_`win'), missing
*gen pre_exp_hhsum_week_`win'=0

*FOR SHOCKS
gen ms_hh_shock_o_source_MM=(ms_hh_shock_o_source_19==1 | ms_hh_shock_o_source_7==1)
replace ms_hh_shock_o_source_MM=. if ms_hh_shock_o==0
replace ms_hh_shock_o_am_`win'=. if ms_hh_shock_o==0

replace ms_ed_reduce_am_`win'=. if ms_ed_children==0 /*making conditional on having children*/

*Pool other occupation with farming
gen hfps_otherwork_dum=1 if hfps_farm_dum==1 | hfps_a4_b8_dum_week==1
replace hfps_otherwork_dum=0 if hfps_farm_dum==0 & hfps_a4_b8_dum_week==0

egen hfps_otherwork_hrs_`win'=rowtotal(hfps_c5_farm_hrs_`win'  hfps_a4_b8_hrs_week_`win'), missing

gen pre_otherwork_dum=1 if pre_farm_dum==1 | pre_a4_b8_dum_week==1
replace pre_otherwork_dum=0 if pre_farm_dum==0 & pre_a4_b8_dum_week==0

egen pre_otherwork_hrs_`win'=rowtotal(pre_c5_farm_hrs_`win'  pre_a4_b8_hrs_week_`win'), missing

gen hfps_dow = dow(hfps_actual_date)

*FOR TRANSFERS
gen hfps_net_transfer_all_`win'=hfps_tr_all_received_am_`win'-hfps_tr_all_given_am_`win'
gen hfps_net_transfer_spouse_`win'=hfps_tr_receive_sp_am_`win' -hfps_tr_give_sp_am_`win'
gen hfps_net_tranfers_mm_`win'=hfps_tr_receive_am_mm_`win' - hfps_tr_give_am_mm_`win' /*net transfers on MM*/


******************
*Run regressions
******************

*Defining globals for all tables

*SAVINGS
	/*Table 4 (with controls) & Table E1 (table 4 w/o controls)*/
local _4_HFPS_dep	d_dep_mmboxIPA_tot_week_`win'  	d_dep_box_ALL_tot_week_bin 			d_dep_box_ALL_tot_week_`win' 	d_dep_mm_ALL_tot_week_bin	 		d_dep_mm_ALL_tot_week_`win'		d_dep_home_week_`win' 		d_dep_bank_week_`win' 		d_dep_svgroup_tot_week_`win'	d_dep_TOTAL_week_`win'  

	/*Table B1 (w/o controls)*/
local _B1_HFPS_wit 	d_wit_mmboxIPA_tot_week_`win'  	d_wit_box_ALL_tot_week_`win'		d_wit_mm_ALL_tot_week_`win'		d_wit_home_week_`win' 		d_wit_bank_week_`win' 			d_wit_TOTAL_week_`win'  


	/*Table B2 (w/o controls)*/
local _B2_MS_wit  	d_wit_mmboxIPA_2m_tot_`win'		d_wit_box_ALL_2m_tot_`win' 			d_wit_mm_ALL_2m_tot_`win' 		d_wit_home_2m_total_`win'  	d_wit_bank_2m_total_`win' 									d_wit_TOTAL_`win' 

	
	/*Table B4 (w/o controls)*/
local _B4_HFPS_nd 	d_nd_mmboxIPA_tot_week_`win'  								 		d_nd_box_ALL_tot_week_`win' 										d_nd_mm_ALL_tot_week_`win'		d_nd_home_week_`win' 		d_nd_bank_week_`win' 		/*d_nd_svgroup_tot_week_`win'*/		d_nd_TOTAL_week_`win'  


	/*Table 5 (with controls) & Table E2 (table 5 w/o controls)*/
local _5_MS_dep  	d_dep_mmboxIPA_2m_tot_`win' 	d_dep_box_ALL_2m_tot_bin		d_dep_box_ALL_2m_tot_`win' 			d_dep_mm_ALL_2m_tot_bin 			d_dep_mm_ALL_2m_tot_`win'		d_dep_home_2m_total_`win'  	d_dep_bank_2m_total_`win' 	d_dep_svgroup_tot_2m_`win'		d_dep_TOTAL_`win' 

	
	/*Table B5 (w/o controls)*/
local _B5_MS_nd  	d_nd_mmboxIPA_2m_tot_`win' 							 			d_nd_box_ALL_2m_tot_`win' 												d_nd_mm_ALL_2m_tot_`win' 		d_nd_home_2m_total_`win'  	d_nd_bank_2m_total_`win' 									d_nd_TOTAL_`win' 


	/*Table B3 (w/o controls)*/
local _B3_MS_bal		d_bal_box_IPA_tot_`win'			d_bal_box_ALL_tot_`win'			d_bal_mm_IPA_tot_`win'				d_bal_mm_ALL_tot_`win'												d_bal_home_`win' 			d_bal_bank_`win'			d_bal_svgroup_`win' 		d_bal_TOTAL_`win' 

	
*BUSINESS
	/*Table 6 (with controls) & Table6_HFPS - Table E3 (table 6 w/o controls)*/
local _6_HFPS 		b1_worked_main_today b1_work_hrs_`win'  a4_b8_dum_week a4_b8_hrs_week_`win' farm_dum c5_farm_hrs_`win' b8_profit_today_`win' b7_revenue_today_`win'      


	/*Table 7 (with controls) & Table E4 (table 7 w/o controls)*/
local _7_MS			b1_work_ b2_hours_r a4_b8_dum_week a4_b8_hrs_week_`win'  c5_farm_dum c5_farm_hrs_week_`win'  b8_profit_day_`win' b7_revenue_day_`win'       

	
*EXPENDITURES

	/*Table 8 (with controls) & Table E5 (table 8 w/o controls)*/
local _8_HFPS 		exp1_week_`win' exp_per_week_`win'  	exp_hh_week_`win' 	exp10_week_`win' exp_tot_week_`win'	


	/*Table 9 (with controls) & Table E6 (table 9 w/o controls)*/
local _9_MS 		exp1_`win' 		exp_per_week_`win'  		exp_hh_week_`win'  exp_educ_daily_`win'	 holiday_spending_tot_w_`win'	exp_total_`win' 	   


	/*Table A7*/
local _A7_MS 		land_purchase land_rent  farm_inputs_`win'


*TRANSFERS
	/*Table 6 (with controls) & Table E3 (table 6 w/o controls)*/
local _6_HFPS_cred 		tr_receive_sp_nonsp_dum 	tr_receive_sp_nonsp_am_`win' 	tr_given_sp_nonps_dum  tr_given_sp_nonps_am_`win' ///
						cr_loan_all_dum 	cr_loan_all_am_`win'
						
*CREDIT
	/*Credit - Table 7 (with controls) & Table E4 (table 7 w/o controls)*/
local _7_MS_cred			tr_receive_sp_nonsp_dum		tr_receive_sp_nonsp_am_`win'		tr_given_sp_nonps_dum	tr_given_sp_nonps_am_`win' ///
						cr_loan_all_dum cr_loan_all_am_`win' 	credit_customers_`win' 						

						
*HF SURVEYING EFFECTS
	/*Table C3*/
local _C3_MS		d_dep_mmboxIPA_2m_tot_`win' d_dep_TOTAL_`win' b1_work_	b2_hours_r  c5_farm_dum c5_farm_hrs_week_`win'    b8_profit_day_`win' b7_revenue_day_`win' exp_total_`win'


*HF Risk Coping - SICKNESS
	/*Table A13 (SELF)*/
local _A13_self_sickness_prev 	hh_sick_self_dum


	/*Table A13 (MEMBER)*/
local _A13_member_sickness_prev 	hh_sick_member_dum

	
	/*Table A12*/
local _A12_sickness_treatment 	hh_sick_self_delay 		hh_sick_self_treat_1 		hh_sick_member_delay 		hh_sick_member_treat_1


*#delimit
gen pre_hh_sick_self_delay=0
gen pre_hh_sick_member_treat_1=0
gen pre_hh_sick_self_treat_1=0
gen pre_hh_sick_member_delay=0


* Baseline Controls
local baseline_controls bl_b20_farm bl_total_asset_value   bl_b23_phonemobile_dum bl_has_mobile_money  bl_sav10_bank_dum bl_sav10_box_dum

gen treatment=1 if control==0
replace treatment=0 if control==1
*#delimit

********************************************
*HFPS  REGRESSIONS
*Tables: 4,6,8,A8,A12,A13,B1,B2,E1,E3,E5,E7
********************************************


foreach controls in no_controls controls {

if "`controls'"=="controls"{
local reg_baseline_controls `baseline_controls'
local control_ending "_controls"
}

if "`controls'"=="no_controls"{
local reg_baseline_controls ""
local control_ending ""
}
foreach spec in individual pooled mm_boxes { /*individual pooled mm_boxes*/

foreach group in _4_HFPS_dep _B4_HFPS_nd _B1_HFPS_wit  ///
					_6_HFPS _8_HFPS _6_HFPS_cred ///
					_A13_self_sickness_prev _A13_member_sickness_prev _A12_sickness_treatment {

					
 local append_replace replace

 preserve
if "`spec'"=="individual"{
	local independent_vars one_box multi_box one_mm multi_mm
	}

	if "`spec'"=="pooled"{
	local independent_vars treatment
	}
	
	if "`spec'"=="mm_boxes"{
	local independent_vars box mobile_money
	}
	local append_replace replace
foreach var of local `group'{

	local add_restriction=""
	local dependent_var hfps_`var'
	local lottery_var "hfps_lottery_week_dum"

	*FOR SAVINGS/EXPENDITURE TABLES - convert dependent variable values from week --> daily values, ignoring binary variables 
		if ("`group'"=="_4_HFPS_dep" |"`group'"=="_B1_HFPS_wit" | "`group'"=="_B4_HFPS_nd" | "`group'"=="_8_HFPS") & strpos("`var'", "_bin")==0{
		qui gen hfps_`var'd=hfps_`var'/7 
		local dependent_var  hfps_`var'd
		}
		
	*FOR BUSINESS TABLE
		if "`group'"=="_6_HFPS"{
			if "`var'"=="b1_worked_main_today" | "`var'"=="b1_work_hrs_`win'" |   "`var'"=="b7_revenue_today_`win'" |  "`var'"=="b8_profit_today_`win'"{
				local lottery_var "hfps_lottery_day_dum"
		}	
		}

	*FOR SAVINGS TABLE ONLY
		if ("`group'"=="_4_HFPS_dep" |"`group'"=="_B1_HFPS_wit" | "`group'"=="_B4_HFPS_nd" ){
		local add_restriction "& hfps_in_regress==1"
		}
		
	*FOR TRANSFERS, but not for transfers table, since we are preserving the original weekly values
		if "`group'"!="_6_HFPS_cred" {
		if "hfps_`var'"=="hfps_tr_receive_sp_nonsp_am_`win'" | "hfps_`var'"=="hfps_tr_given_sp_nonps_am_`win'"{
		qui gen hfps_`var'd=hfps_`var'/7 
		local dependent_var  hfps_`var'd
		}
		}
			
	

	qui xi: reg `dependent_var' `independent_vars' pre_`var' `lottery_var'  i.strata i.hfps_actual_date  `reg_baseline_controls' [pweight=weight_inv] ///
		 if (one_box==1 | multi_box==1 | mobile_money==1 | control==1)  `add_restriction' `round_restriction', cluster(id)

		 
		 local clusters=e(N_clust)	
		 
		qui sum `dependent_var' if e(sample) & control==1
		local mean=r(mean) 		
		local sd=r(sd)	
							 
		 
	*A. Combined MM and Boxed - separate for each account
			if "`spec'"=="individual"{
		
				qui test one_box=multi_box 						
				local p_box_one_multi=r(p)			
				
				qui test one_box multi_box							
				local p_box_joint=r(p)			
				
				qui test one_mm=multi_mm							
				local p_mm_one_multi=r(p)			
				
				qui test one_mm multi_mm							
				local p_mm_joint=r(p)			
				
				qui test one_mm multi_mm one_box multi_box			
				local p_all_joint=r(p)

				*for MHA
				qui test one_box=0									
				local p_one_box=r(p)
				
				qui test multi_box=0								
				local p_multi_box=r(p)	
				
				qui test one_mm=0									
				local p_one_mm=r(p)
				
				qui test multi_mm =0								
				local p_multi_mm=r(p)
				
				
				outreg2 using "${path2}\table`group'`control_ending'.out", `append_replace' ///
				 nonote se symb(***,**,*)  dec(2) nocons ///
				 addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters', ///
				 "T-test one box = multi box", `p_box_one_multi', ///
				 "T-test joint one box multi box", `p_box_joint', ///
				 "T-test one mm = multi mm", `p_mm_one_multi', ///
				 "F-test joint one mm multi mm", `p_mm_joint', ///
				 "F-test joint all accounts", `p_all_joint', ///
				 "T-test_p_one_box", `p_one_box', ///
				 "T-test_p_multi_box", `p_multi_box', ///
				 "T-test_p_one_mm", `p_one_mm', ///
				 "T-test_p_multi_mm", `p_multi_mm') ///
				 keep(one_box multi_box one_mm multi_mm) slow(100)
		
			 }

		*A2.  Combined MM and Boxed - pooled
			 if "`spec'"=="pooled"{
					 
				*for MHA
				qui test treatment=0						
				local p_treatment=r(p)
							
				outreg2 using "${path2}\table`group'_pooled`control_ending'.out", `append_replace' ///
				nonote se symb(***,**,*)  dec(2) nocons ///
				addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters',"T-treatment", `p_treatment') ///
				keep(treatment) slow(100)
			}	 
			 
			 			
		*A3 - Separate MM and boxes
		
				if "`spec'"=="mm_boxes"{
				if ("`group'"!="_A12_sickness_treatment" &"`group'"!="_A13_member_sickness_prev" &"`group'"!="_A13_self_sickness_prev") {
				
				qui	test box=0										
				local p_box=r(p)
				
				qui	test mobile_money=0							
				local p_mm=r(p)
				
				qui test box mobile_money							
				local p_joint_box_mm=r(p)
				
				qui test box=mobile_money							
				local p_box_mm=r(p)
				
				outreg2 using "${path2}\table`group'_panelB`control_ending'.out", `append_replace'  ///
				nonote se symb(***,**,*)  dec(2) nocons ///
				addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters', ///
				"T_box", `p_box', ///
				"T_mm", `p_mm', ///
				"T_box_mm_joint", `p_joint_box_mm', ///
				"T_box_mm",  `p_box_mm') ///
				keep(box mobile_money) slow(100)
					
			     }
				}
			
				local append_replace append
 
		 }
		 restore
		 }	
	 	 }
		 }





*********************************************
*MS Regressions
*Tables: 5,7,9,A7,A9,B2,B4,B5,C3,E2,E4,E6,E8
*********************************************


*#delimit
foreach controls in no_controls controls {

if "`controls'"=="controls"{
local reg_baseline_controls `baseline_controls'
local control_ending "_controls"
}

if "`controls'"=="no_controls"{
local reg_baseline_controls ""
local control_ending ""
}

foreach spec in  individual pooled mm_boxes { /*individual pooled mm_boxes*/

foreach group in _C3_MS  _5_MS_dep _B5_MS_nd ///
	_B3_MS_bal _B2_MS_wit  _7_MS _9_MS  _A7_MS _7_MS_cred {	

	
	local append_replace replace
	
	if "`spec'"=="individual"{
	local independent_vars one_box multi_box one_mm multi_mm
	}

	if "`spec'"=="pooled"{
	local independent_vars treatment
	}
	
	if "`spec'"=="mm_boxes"{
	local independent_vars box mobile_money
	}

	
		foreach var of local `group'{
		preserve

			local dependent_var  ms_`var'
			qui cap gen ms_`var'_d=ms_`var' 
			
		

	*FOR SAVINGS/HF TABLES - convert dependent variable values from 2 month --> daily values
	if ("`group'"=="_5_MS_dep" | "`group'"=="_B2_MS_wit" | "`group'"=="_B5_MS_nd" |  "`group'"=="_B5_MS_nd") & strpos("`var'", "_bin")==0{
		*convert to daily
			qui replace ms_`var'_d=ms_`var'/60 /*converting bi-monthly to daily*/
		local dependent_var  ms_`var'_d
		}
	
	*FOR BUSINES TABLES
	if ("`group'"=="_7_MS" | "`group'"=="_C3_MS") & ("`var'"=="b1_work_" | "`var'"=="b2_hours_r" )  {
		keep  mobile_money one_box multi_box one_mm multi_mm ms_b1_work_* ms_b2_hours_r* strata ms_round ms_call_date weight_inv control id bl_hf pre* *farm_inputs_`win'  *land_purchase *land_rent *hfps treatment box mobile_money `baseline_controls'
		gen k=_n
		reshape long ms_b1_work_ ms_b2_hours_r, i(k) j(day) /*reshaping to get daily observation for labor supply in MS*/
		qui sum ms_b2_hours_r if ms_b2_hours_r!=0,d
		replace ms_b2_hours_r=r(p95) if ms_b2_hours_r>r(p95) & !missing(ms_b2_hours_r) /*winsorizing*/
		gen  ms_`var'_d=ms_`var'
		}
			
		
 	
	*FOR TRANSFERS, but not for transfers table, since we are preserving the original weekly values
	if "`group'"=="_7_MS_cred" {
	if 		("ms_`var'"=="ms_tr_receive_sp_nonsp_am_`win'" | "ms_`var'"=="ms_tr_given_sp_nonps_am_`win'" | ///
			"ms_`var'"=="ms_credit_customers_`win'") {
		*convert to daily
			qui replace ms_`var'_d=ms_`var'/30 /*converting monthly to daily*/
			local dependent_var  ms_`var'_d
		}
		local dependent_var  ms_`var'_d
		}
	
	
	*FOR EXPENDITURES/HF TABLES	
	if ("`group'"=="_9_MS" ){
		if 	  "`var'"!="exp_educ_`win'" &  	"`var'"!="land_purchase" ///		
			&  "`var'"!="land_rent"		&  "`var'"!="farm_inputs_`w5'"	&  "`var'"!="assets_dum"{ /*convering all variables to daily except for holidays*/
		qui replace ms_`var'_d=ms_`var'/7
		}
		local dependent_var  ms_`var'_d
		}
		
	*INTERACTIVE EFFECTS WITH HFPS	
	if "`group'"=="_C3_MS" | "`group'"=="Table10b_MS"{
	local independent_vars hfps one_box multi_box one_mm multi_mm one_box_hfps multi_box_hfps one_mm_hfps multi_mm_hfps 
	
		if 	"`var'"=="holiday_spending_tot_`win'" { /*holidays are converted to monthly*/
		qui replace ms_`var'_d=ms_`var'/3 
		}
			
		if 	"`var'"=="exp_hhsum_week_`win'" | "`var'"=="b8_profit_today_`win'" { /*expenses are converted from weekly to daily*/
		qui replace ms_`var'_d=ms_`var'/7 
		}	
		
		if "`var'"=="d_dep_mmboxIPA_2m_tot_`win'" | "`var'"=="d_dep_TOTAL_`win'"{
		qui replace ms_`var'_d=ms_`var'/60  /*deposits are converted from 2 months to daily*/
		}
		local dependent_var  ms_`var'_d
		}

	
		qui xi: reg `dependent_var' `independent_vars' pre_`var' `reg_baseline_controls' i.strata i.ms_call_date bl_hf [pweight=weight_inv] ///
			if (one_box==1 | multi_box==1 | mobile_money==1 | control==1) `round_restriction', cluster(id)
			
			local clusters=e(N_clust)
			
			qui sum `dependent_var' if e(sample) & control==1
			local mean=r(mean)								
			local sd=r(sd)	
		
		*A. Combined MM and Boxed
				if "`spec'"=="individual"{
					
					qui test one_box=multi_box						
					local p_box_one_multi=r(p)	
					
					qui test one_box multi_box						
					local p_box_joint=r(p)		
					
					qui test one_mm=multi_mm						
					local p_mm_one_multi=r(p)		
					
					qui test one_mm multi_mm						
					local p_mm_joint=r(p)		
					
					qui test one_mm multi_mm one_box multi_box		
					local p_all_joint=r(p)
					
					*for MHA
					qui test one_box=0							
					local p_one_box=r(p)
					
					qui test multi_box=0						
					local p_multi_box=r(p)	
					
					qui test one_mm=0							
					local p_one_mm=r(p)	
					
					qui test multi_mm =0						
					local p_multi_mm=r(p)
										
					
					if "`group'"=="_C3_MS"{
					qui test one_mm_hfps multi_mm_hfps one_box_hfps multi_box_hfps
					local p_all_hf_joint=r(p)
					local hf_stats ", "F-test joint - all interaction" , `p_all_hf_joint'"
					}
					
					outreg2 using "${path2}\table`group'`control_ending'.out", `append_replace'  ///
					nonote se symb(***,**,*)  dec(2) nocons ///
					addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters', ///
					"T-test one box = multi box", `p_box_one_multi', ///
					 "T-test joint one box multi box", `p_box_joint', ///
					 "T-test one mm = multi mm", `p_mm_one_multi', ///
					 "F-test joint one mm multi mm", `p_mm_joint', ///
					 "F-test joint all accounts", `p_all_joint', ///
					 "T-test_p_one_box", `p_one_box', ///
					 "T-test_p_multi_box", `p_multi_box', ///
					 "T-test_p_one_mm", `p_one_mm', ///
					 "T-test_p_multi_mm", `p_multi_mm') ///
					keep(one_box multi_box one_mm multi_mm one_box_hfps multi_box_hfps one_mm_hfps multi_mm_hfps hfps)
				
				}				
	
		
		*A2.  Combined MM and Boxed - pooled
		
				if "`spec'"=="pooled"{
				
					qui test treatment=0						
					local p_treatment=r(p)
			
					outreg2 using "${path2}\table`group'_pooled`control_ending'.out", append /// 
					nonote se symb(***,**,*)  dec(2) nocons ///
					addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters', ///
					"T-treatment", `p_treatment') ///
					keep(treatment)
				
				}
	
				
		*A3 - Separate MM and boxes
		
				if "`spec'"=="mm_boxes"{
				if "`group'"!="_C3_MS" {
					
					qui test box=0										
					local p_box=r(p)
					
					qui test mobile_money=0							
					local p_mm=r(p)
					
					qui test box mobile_money							
					local p_joint_box_mm=r(p)
					
					qui test box=mobile_money							
					local p_box_mm=r(p)
					
					outreg2 using "${path2}\table`group'_panelB`control_ending'.out", `append_replace'  ///
					nonote se symb(***,**,*)  dec(2) nocons ///
					addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters', ///
					"T_box", `p_box', ///
					"T_mm", `p_mm', ///
					"T_box_mm_joint", `p_joint_box_mm', ///
					"T_box_mm",  `p_box_mm') ///
					keep(box mobile_money)
                   }
				}
		
	 drop ms_`var'_d
	 local append_replace append
	
	 restore

		 }
		 }
		}
		}

		 




***********************************************************************
*Table A11 - (HFPS)  Effect of accounts on coping with health shocks
***********************************************************************

*#delimit
* Running Regression

local append_replace replace
foreach self in self member{ 

*A. Combined MM and Boxed

	 
	 foreach var in net_transfer_all_`win' d_nd_mmboxIPA_tot_week_`win' d_nd_TOTAL_week_`win' ///
	 exp_med_week_`win' exp1_week_`win' exp_tot_week_`win' all_hrs_week_`win'  all_income_week_`win' {
	 	 
		qui xi: xtreg hfps_`var' hfps_hh_sick_`self'_dum /// 
			sick_`self'Xone_box sick_`self'Xmulti_box  sick_`self'Xone_mm sick_`self'Xmulti_mm ///
			hfps_lottery_week_dum i.hfps_actual_date ///
		 [pweight=weight_inv] if (one_box==1 | multi_box==1 | mobile_money==1 | control==1), i(id) fe cluster(id)
		 
			
			qui test sick_`self'Xone_box sick_`self'Xmulti_box
			local p_box_joint=r(p)
			
		
		qui sum hfps_`var' if e(sample) & control==1 &  hfps_hh_sick_`self'_dum==0
		local mean=r(mean)
		local sd=r(sd)
		local clusters=e(N_clust)
		

		outreg2 using "${path2}\tableA11_sickness.out", `append_replace' ///
		 nonote se symb(***,**,*)  dec(2) nocons ///
		 addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters', "T-test joint one box multi box", `p_box_joint') ///
		 keep(hfps_hh_sick_`self'_dum sick_`self'Xone_box sick_`self'Xmulti_box  sick_`self'Xone_mm sick_`self'Xmulti_mm )
		
		local append_replace append

		 }
		 
		 }

		 
	 
*********************************************************************
*Gnerating Table D5 - (HFPS) EFFECTS OF LOTTERY PAYMENTS ON OUTCOMES
*********************************************************************

local tableD5_HFPS d_dep_mmboxIPA_tot_week_`win' d_dep_TOTAL_week_`win' b1_worked_main_today b1_work_hrs_`win' farm_dum c5_farm_hrs_`win' b8_profit_week_`win'  b7_revenue_week_`win'  exp_tot_week_`win' /*b8_profit_today_`win'  b7_revenue_today_`win' 	b6_inv_equip_`win'*/

local append_replace replace

foreach var of local tableD5_HFPS{

*FOR DAILY OUTCOMES
	if "`var'"=="b1_worked_main_today" |  "`var'"=="b1_work_hrs_`win'" |  "`var'"=="b8_profit_today_`win'" |  "`var'"=="b7_revenue_today_`win'"{
	local independent_vars "hfps_lottery_day_dum hfps_lottery_dayXone_box hfps_lottery_dayXmulti_box  hfps_lottery_dayXone_mm hfps_lottery_dayXmulti_mm"
	
	qui xi: xtreg hfps_`var' `independent_vars' ///
			i.hfps_actual_date ///
		 [pweight=weight_inv] if (one_box==1 | multi_box==1 | mobile_money==1 | control==1), i(id) fe cluster(id)
		
		qui test hfps_lottery_dayXone_mm hfps_lottery_dayXmulti_mm hfps_lottery_dayXone_box hfps_lottery_dayXmulti_box			
		local p_all_joint=r(p)
		
		qui sum hfps_`var'  if e(sample) & control==1 & hfps_lottery_day_dum==0			
		local mean=r(mean)					
		local sd=r(sd)			
		local clusters=e(N_clust)

		 }
		
*FOR WEEKLY OUTCOMES		 
	if "`var'"!="b1_worked_main_today" &  "`var'"!="b1_work_hrs_`win'" & "`var'"!="b8_profit_today_`win'" & "`var'"!="b7_revenue_today_`win'"{
	local independent_vars "hfps_lottery_week_dum hfps_lottery_weekXone_box hfps_lottery_weekXmulti_box  hfps_lottery_weekXone_mm hfps_lottery_weekXmulti_mm"
	qui xi: xtreg hfps_`var' `independent_vars' ///
			i.hfps_actual_date ///
		 [pweight=weight_inv] if (one_box==1 | multi_box==1 | mobile_money==1 | control==1), i(id) fe cluster(id)
		 
		
		qui test hfps_lottery_weekXone_mm hfps_lottery_weekXmulti_mm hfps_lottery_weekXone_box hfps_lottery_weekXmulti_box
		local p_all_joint=r(p)
		
		qui sum hfps_`var'  if e(sample) & control==1 & hfps_lottery_week_dum==0
		local mean=r(mean)
		local sd=r(sd)
		local clusters=e(N_clust)

		 }

	outreg2 using "${path2}\tableD5_HFPS.out", `append_replace'  ///
		 nonote se symb(***,**,*)  dec(2) nocons ///
		 addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters', "F-test joint all accounts", `p_all_joint') ///
		 keep(`independent_vars')
	local append_replace append
		
}
	 
	

	 
****************************************
*Generating Table D1-D4 - Heterogeneity
****************************************
	
local HFPS 	d_dep_mmboxIPA_tot_week_`win' d_dep_TOTAL_week_`win'
local MS 	d_dep_mmboxIPA_2m_tot_`win' d_dep_TOTAL_`win'

local reg_baseline_controls `baseline_controls'

*female taxed

foreach het in female taxed mm box tp_inconsistent{  /*female taxed mm box tp_inconsistent*/

foreach group in MS  HFPS{

local append_replace replace

foreach var of local `group'{

disp "`group'_`het'_controls"


	if "`group'"=="MS"{
	preserve
	
	foreach v in  ms_d_dep_mmboxIPA_2m_tot_`win' ms_d_dep_TOTAL_`win' {
	replace `v'=`v'/60 /*converting to daily*/
	} 
	
	if ("`het'"=="mm" | "`het'"=="box") {
	local t_no D4
	}
	
	if ("`het'"=="taxed" | "`het'"=="female" | "`het'"=="tp_inconsistent") {
	local t_no D2
	}
	
	replace ms_exp_total_`win'=ms_exp_total_`win'/7

		
			qui xi: reg ms_`var' one_box multi_box one_mm multi_mm   bl_`het' bl_`het'Xone_box bl_`het'Xmulti_box bl_`het'Xone_mm bl_`het'Xmulti_mm  ///
										i.strata i.ms_call_date bl_hf `reg_baseline_controls' [pweight=weight_inv] if (one_box==1 | multi_box==1 | mobile_money==1 | control==1) , cluster(id)
			qui test one_box multi_box one_mm multi_mm
			local p_f_accounts=r(p)
			
			qui test bl_`het'Xone_box bl_`het'Xmulti_box bl_`het'Xone_mm bl_`het'Xmulti_mm bl_`het'
			local p_f_accounts_`het'=r(p)
			
			qui sum ms_`var' if e(sample) & control==1 & bl_`het'==0
			local mean=r(mean)								
			local sd=r(sd)					
			local clusters=e(N_clust)
		
	
			outreg2 using "$path2\table_`t_no'_`group'_`het'_controls.out", `append_replace'  ///
				 nonote se symb(***,**,*)  dec(2) nocons ///
				 addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters', ///
				 "F joint one_box multi_box one_mm multi_mm", `p_f_accounts', ///
				 "F joint one_box multi_box one_mm multi_mm `het'", `p_f_accounts_`het'') ///
				 keep(one_box multi_box one_mm multi_mm   bl_`het' bl_`het'Xone_box bl_`het'Xmulti_box bl_`het'Xone_mm bl_`het'Xmulti_mm)
			 
		restore 	
			}
				
	if "`group'"=="HFPS"{
	preserve
	
		foreach v in  hfps_d_dep_mmboxIPA_tot_week_`win' hfps_d_dep_TOTAL_week_`win' hfps_exp_tot_week_`win' {
		replace `v'=`v'/7 /*converting to daily*/
		}

		if ("`het'"=="mm" | "`het'"=="box") {
		local t_no D3
		}
		if ("`het'"=="taxed" | "`het'"=="female" | "`het'"=="tp_inconsistent") {
		local t_no D1
		}
		
		local lottery_var "hfps_lottery_day_dum"
		if "`var'"=="d_dep_mmboxIPA_tot_week_`win'" | "`var'"== "d_dep_TOTAL_week_`win'" |  "`var'"== "farm_dum" |  "`var'"== "c5_farm_hrs_`win'" | "`var'" =="exp_tot_week_`win'"  {
		local lottery_var "hfps_lottery_week_dum"
		}
		
			qui xi: reg hfps_`var'  ///
					one_box multi_box one_mm multi_mm ///   
					bl_`het' bl_`het'Xone_box bl_`het'Xmulti_box bl_`het'Xone_mm bl_`het'Xmulti_mm ///
					pre_`var' `lottery_var'  i.strata i.hfps_actual_date  `reg_baseline_controls'  [pweight=weight_inv] ///
											if (one_box==1 | multi_box==1 | mobile_money==1 | control==1) , cluster(id)
	
			qui test one_box multi_box one_mm multi_mm
			local p_f_accounts=r(p)
			
			qui test bl_`het'Xone_box bl_`het'Xmulti_box bl_`het'Xone_mm bl_`het'Xmulti_mm bl_`het'
			local p_f_accounts_`het' =r(p)
			
			qui sum hfps_`var' if e(sample) & control==1 & bl_`het'==0
			local mean=r(mean)								
			local sd=r(sd)					
			local clusters=e(N_clust)

			
			outreg2 using "$path2\table_`t_no'_`group'_`het'_controls.out", `append_replace'  ///
				 nonote se symb(***,**,*)  dec(2) nocons ///
				 addstat("control mean", `mean', "control sd", `sd', "clusters", `clusters', ///
				 "F joint one_box multi_box one_mm multi_mm", `p_f_accounts', ///
				 "F joint one_box multi_box one_mm multi_mm `het'", `p_f_accounts_`het'') ///
				 keep(one_box multi_box one_mm multi_mm   bl_`het' bl_`het'Xone_box bl_`het'Xmulti_box bl_`het'Xone_mm bl_`het'Xmulti_mm)
			 
		restore 
			}		
		local append_replace append
	}
}
}
	

	
*******************************************
*Tables A5 and A6 -  FDR-Adjusted Q-Values
*******************************************


local table_list_hfps ///
 _4_HFPS_dep ///
 _6_HFPS	///		
 _8_HFPS   ///	
 _6_HFPS_cred  ///


local table_list_ms ///
 _5_MS_dep	///	
 _7_MS	   ///		
 _9_MS 	  ///		
 _7_MS_cred   ///

 
*****************************************************
*Running Corrections for Individual and All Accounts
*****************************************************
 
 foreach group in 1 2 3  {
	if "`group'"=="1"{ /*individual treatments*/
		local ending ""
	}	
	if "`group'"=="2"{ /*pooled*/
		local ending "_pooled"
    }
	if "`group'"=="3"{ /*mm_boxes joint*/
		local ending "_panelB"
	}
 
 foreach list in hfps ms{
 if "`list'"=="hfps" {
 loc t_no A5
 }
 if "`list'"=="ms" {
 loc t_no A6
 }
 
 foreach table of local table_list_`list'{
  insheet using "${path2}\\table`table'`ending'_controls.out", clear


	if "`group'"=="1"{ /*individual treatments*/
		
		keep if v1=="VARIABLES"  | ///
			v1=="T-test one box = multi box" |				v1=="T-test joint one box multi box" | ///
			v1=="T-test one mm = multi mm" |				v1=="F-test joint one mm multi mm" | ///
			v1=="F-test joint all accounts" |				v1=="T-test_p_one_box" | ///
			v1=="T-test_p_multi_box" |						v1=="T-test_p_one_mm"| ///
			v1=="T-test_p_multi_mm"
	
	}
	
	if "`group'"=="2"{ /*pooled*/
	
	keep if v1=="VARIABLES"  | ///
		v1=="T-treatment"
	}	


	if "`group'"=="3"{ /*mm_boxes joint*/
		
		keep if v1=="VARIABLES"  | ///
			v1=="T_box" | ///
			v1=="T_mm" 
	}	

		replace v1 = subinstr(v1, " ", "_",.)
		replace v1 = subinstr(v1, "-", "_",.)
		replace v1 = subinstr(v1, "=", "equal",.)
		rename v1 descrip 

		reshape long v, i(descrip)
		sort descrip

		reshape wide v, i(_j) j(descrip) string

		sort _j
		gen var_table_order=_n
		drop _j
		
		rename vVARIABLES variable
		order variable *	
	
				if "`group'"=="1"{
				rename vT_test_one_box_equal_multi_box v1 
				rename vT_test_joint_one_box_multi_box v2
				rename vT_test_one_mm_equal_multi_mm v3
				rename vF_test_joint_one_mm_multi_mm v4
				rename vF_test_joint_all_accounts v5
				rename vT_test_p_one_box v6
				rename vT_test_p_multi_box v7
				rename vT_test_p_one_mm v8
				rename vT_test_p_multi_mm v9
				}
				
				if "`group'"=="3"{
				rename vT_box v1
				rename vT_mm v2
				}

				
				if "`group'"=="2"{
				rename vT_treatment v1

				}

		reshape long v, i(variable var_table_order)
		
		gen test=""
				if "`group'"=="1"{
				replace test="T-test one box = multi box" if _j==1
				replace test="F_test_joint_box" if _j==2
				replace test="T_test_joint_one_mm_multi_mm" if _j==3
				replace test="F_test_joint_mm" if _j==4
				replace test="F_test_joint_all_accounts" if _j==5
				replace test="T_test_p_one_box" if _j==6
				replace test="T_test_p_multi_box" if _j==7
				replace test="T_test_p_one_mm" if _j==8
				replace test="T_test_p_multi_mm" if _j==9
				}
				if "`group'"=="3"{
				replace test="T_test_box" if _j==1
				replace test="T_test_mm" if _j==2
				}

				if "`group'"=="2"{
				replace test="T_all" if _j==1
				}

		rename _j test_order

		gen pval=v
		replace pval=subinstr(p, "(", "",.)
		replace pval=subinstr(p, ")", "",.)
		destring pval, replace
		
		drop v

		gen table="table`table'"
		
		gen spec=""
		
		*excluding transfers because they were not in PAP	
	  drop if variable=="hfps_tr_given_sp_nonps_am_w5" | ///
		variable=="hfps_tr_given_sp_nonps_dum" | ///
		variable=="hfps_tr_given_sp_nonps_dum" | ///
		variable=="hfps_tr_receive_sp_nonsp_am_w5" | ///
		variable=="hfps_tr_receive_sp_nonsp_am_w5" | ///
		variable=="hfps_tr_receive_sp_nonsp_dum" | ///
		variable=="ms_tr_given_sp_nonps_am_w5" | ///
		variable=="ms_tr_given_sp_nonps_dum" | ///
		variable=="ms_tr_receive_sp_nonsp_am_w5" | ///
		variable=="ms_tr_receive_sp_nonsp_dum"
		
	*excluding holiday spending - wasn't part of PAP	
		drop if variable=="ms_holiday_spending_tot_w5_d" /*wasn't a part of PAP*/
		
	*dropping variables that do not need to be included in the adjustment
		if "`table'"=="_4_HFPS_dep" | "`table'"=="_5_MS_dep"{
			keep if variable=="hfps_d_dep_TOTAL_week_w5d" | variable=="ms_d_dep_TOTAL_w5_d" | ///
			variable=="ms_d_dep_mm_ALL_2m_tot_w5_d" | variable=="ms_d_dep_box_ALL_2m_tot_w5_d" | ///
			 variable=="hfps_d_dep_box_ALL_tot_week_w5d" | variable=="hfps_d_dep_mm_ALL_tot_week_w5d" 
			}

		
		tempfile table`table'_`list'
		save 	`table`table'_`list''
		

}

clear	
	foreach table of local table_list_`list'{
	append using `table`table'_`list'', force
	
	}
	
	gen survey="`list'"

	tempfile master_`list'
	save `master_`list''


					
		if "`group'"=="1"{		
		keep if test=="T_test_p_multi_box" | ///
				test=="T_test_p_multi_mm" | ///
				test=="T_test_p_one_box" | ///
				test=="T_test_p_one_mm" 	
		local order ///
		pval_T_test_p_one_box 		qvalue_T_test_p_one_box ///
		pval_T_test_p_multi_box 	qvalue_T_test_p_multi_box ///	
		pval_T_test_p_one_mm 		qvalue_T_test_p_one_mm ///
		pval_T_test_p_multi_mm 		qvalue_T_test_p_multi_mm
		}
	
		if "`group'"=="2"{		
		local order ///
		pval_T_all 		qvalue_T_all
		}
		
		if "`group'"=="3"{		
		local order ///
		pval_T_test_box 		qvalue_T_test_box ///
		pval_T_test_mm 			qvalue_T_test_mm
		}
		
		
		* Collect the total number of p-values tested

		quietly sum pval
		local totalpvals = r(N)

		* Sort the p-values in ascending order and generate a variable that codes each p-value's rank

		quietly gen int original_sorting_order = _n
		quietly sort pval
		quietly gen int rank = _n if pval~=.

		* Set the initial counter to 1 

		local qval = 1

		* Generate the variable that will contain the BKY (2006) sharpened q-values

		gen bky06_qval = 1 if pval~=.

		* Set up a loop that begins by checking which hypotheses are rejected at q = 1.000, then checks which hypotheses are rejected at q = 0.999, then checks which hypotheses are rejected at q = 0.998, etc.  
		*The loop ends by checking which hypotheses are rejected at q = 0.001.

		while `qval' > 0 {
			* First Stage
			* Generate the adjusted first stage q level we are testing: q' = q/1+q
			local qval_adj = `qval'/(1+`qval')
			*gen qval_adj = `qval'/(1+`qval')
			* Generate value q'*r/M
			gen fdr_temp1 = `qval_adj'*rank/`totalpvals'
			* Generate binary variable checking condition p(r) <= q'*r/M
			gen reject_temp1 = (fdr_temp1>=pval) if pval~=.

			* Generate variable containing p-value ranks for all p-values that meet above condition
			gen reject_rank1 = reject_temp1*rank
			* Record the rank of the largest p-value that meets above condition
			egen total_rejected1 = max(reject_rank1)
			* Second Stage
			* Generate the second stage q level that accounts for hypotheses rejected in first stage: q_2st = q'*(M/m0)
			local qval_2st = `qval_adj'*(`totalpvals'/(`totalpvals'-total_rejected1[1]))
			* Generate value q_2st*r/M
			gen fdr_temp2 = `qval_2st'*rank/`totalpvals'
			* Generate binary variable checking condition p(r) <= q_2st*r/M
			gen reject_temp2 = (fdr_temp2>=pval) if pval~=.
			* Generate variable containing p-value ranks for all p-values that meet above condition
			gen reject_rank2 = reject_temp2*rank
			* Record the rank of the largest p-value that meets above condition
			egen total_rejected2 = max(reject_rank2)

			* A p-value has been rejected at level q if its rank is less than or equal to the rank of the max p-value that meets the above condition
			replace bky06_qval = `qval' if rank <= total_rejected2 & rank~=.
			* Reduce q by 0.001 and repeat loop
			drop fdr_temp* reject_temp* reject_rank* total_rejected*
			local qval = `qval' - .001
		}
			

		quietly sort original_sorting_order
		
			rename bky06_qval qvalue_
			order table variable test pva qval *
			tostring var_table_order, replace
			gen variable1=var_table_order +". " + variable
			drop variable
			rename variable1 variable
			keep table variable test pval qva var_table_order test_order
			rename pval pval_
			sort table variable var_table_order test_order
			drop test_order
			
			*save  "${MAP2_RCT_results_final_combined}\\`hyp'_`list'.dta", replace

			reshape wide pval qval , i(table variable var_table_order) j(test ) str	
			
			order table variable `order'

			outsheet  using /// 
				"${path2}\table_`t_no'_sig_sharpened_group`group'_`list'.out", replace
				}
				}
	
		
	
	
************************************************************************************************
* Table C1 - Correlations between Self-Reports and Administrative Data for Mobile Money Groups
***********************************************************************************************

*#delimit
*1. MM
	use "${path1}MAP2_Working_File.dta", clear

	*replacing with missing for people who didn't have 2nd MM account
	replace hfps_d_dep_mm2_today=. if pre_num_mm_acc_final<2
	replace hfps_d_dep_mm2_week=. if pre_num_mm_acc_final<2	
	
	*mm accounts
	
	rename hfps_d_dep_mm1_today 	hfps_d_dep_mm_1_today
	rename hfps_d_dep_mm2_today 	hfps_d_dep_mm_2_today
	rename hfps_d_dep_mm2_week 		hfps_d_dep_mm_2_week
	rename hfps_d_dep_mm1_week 		hfps_d_dep_mm_1_week
	
	rename at_mm1_cashin_amt_daily at_mm_1_cashin_amt_daily
	rename at_mm2_cashin_amt_daily at_mm_2_cashin_amt_daily

	egen hfps_d_dep_mm_both_today =rowtotal(hfps_d_dep_mm_1_today hfps_d_dep_mm_2_today), missing	 /*`account'*/
	egen hfps_d_dep_mm_both_week = rowtotal(hfps_d_dep_mm_1_week hfps_d_dep_mm_2_week), missing
	
	rename at_mm1_running_balance_ms_date at_mm_1_run_bal_ms_date
	rename at_mm2_running_balance_ms_date at_mm_2_run_bal_ms_date
	rename at_mm_running_balance_ms_date at_mm_both_run_bal_ms_date
			
	
	rename ms_d_bal_mm1 ms_d_bal_mm_1
	rename ms_d_bal_mm2 ms_d_bal_mm_2
	rename ms_d_bal_mm_ipa ms_d_bal_mm_both
		
	
	foreach account in 1 2 both{
			
	*daily
		*at
		gen at_mm_`account'_cashin_amt_daily_bin=(at_mm_`account'_cashin_amt_daily>0)
		replace at_mm_`account'_cashin_amt_daily_bin=. if missing(at_mm_`account'_cashin_amt_daily)
		
		*hfps
		gen hfps_d_dep_mm_`account'_today_bin=(hfps_d_dep_mm_`account'_today>0)
		replace hfps_d_dep_mm_`account'_today_bin=. if missing(hfps_d_dep_mm_`account'_today)
	
	*weekly
		*at
	
		bysort id hfps_round hfps_week_ind: egen at_mm_`account'_cashin_amt_weekly=total(at_mm_`account'_cashin_amt_daily) if !missing(hfps_week_ind) & mobile_money==1
		
		*at
		gen at_mm_`account'_cashin_amt_weekly_bin=(at_mm_`account'_cashin_amt_weekly>0)
		replace at_mm_`account'_cashin_amt_weekly_bin=. if missing(at_mm_`account'_cashin_amt_weekly)
		
		*hfps
		gen hfps_d_dep_mm_`account'_week_bin=(hfps_d_dep_mm_`account'_week>0)
		replace hfps_d_dep_mm_`account'_week_bin=. if missing(hfps_d_dep_mm_`account'_week)
		
	*round
		bysort id hfps_round: egen at_mm_`account'_cashin_amt_round=total(at_mm_`account'_cashin_amt_daily), missing
		bysort id hfps_round: egen hfps_d_dep_mm_`account'_round=total(hfps_d_dep_mm_`account'_week), missing
	
	
	
	*balances
		
		gen at_mm_`account'_run_bal_ms_date_bin=(at_mm_`account'_run_bal_ms_date>0) if !missing(at_mm_`account'_run_bal_ms_date)
		gen ms_d_bal_mm_`account'_bin=(ms_d_bal_mm_`account'>0) if !missing(ms_d_bal_mm_`account')
}

	gen asinh_reported_all_mm=asinh(ms_d_bal_mm_both ) 
	gen asinh_validated_all_mm=asinh(at_mm_both_run_bal_ms_date)

	bysort id hfps_round: gen n=_n
	replace at_mm_both_cashin_amt_round=. if n!=1
	replace at_mm_1_cashin_amt_round=. if n!=1
	replace at_mm_2_cashin_amt_round=. if n!=1
	
	replace hfps_d_dep_mm_1_round=. if n!=1
	replace hfps_d_dep_mm_2_round=. if n!=1
	replace hfps_d_dep_mm_both_round=. if n!=1
		
	sort hfps_round id
	reg at_mm_both_cashin_amt_round hfps_d_dep_mm_both_round if hfps_round==1, nocon

	*Long term usage variables

	foreach dep in dep wit bal{
	cap gen el2_`dep'_brown_mm=0
	egen el2_`dep'_all_mm_aug19=rowtotal(el2_`dep'_silver_mm el2_`dep'_black_mm el2_`dep'_brown_mm) if el2_consent_yes==1, missing
	gen el2_`dep'_all_mm_aug19_dum=(el2_`dep'_all_mm_aug19>0) if !missing(el2_`dep'_all_mm_aug19) & el2_consent_yes==1
	}
	gen el2_uses_atleast1mm=(el2_dep_all_mm_aug19_dum==1 | el2_wit_all_mm_aug19_dum==1 | el2_bal_all_mm_aug19_dum==1) if !missing(el2_dep_all_mm_aug19_dum) & el2_consent_yes==1

	rename el2_dep_all_mm_aug19 hfps_d_dep_mm_both_el2_aug19
	rename at_cashin_amt_8_2019 at_mm_both_cashin_el2_aug19
	
	gen hfps_d_dep_mm_both_el2_aug19_bin=hfps_d_dep_mm_both_el2_aug19
	replace hfps_d_dep_mm_both_el2_aug19_bin=1 if hfps_d_dep_mm_both_el2_aug19_bin>0 & !missing(hfps_d_dep_mm_both_el2_aug19_bin)
	
	gen at_mm_both_cashin_el2_aug19_bin=at_mm_both_cashin_el2_aug19
	replace at_mm_both_cashin_el2_aug19_bin=1 if at_mm_both_cashin_el2_aug19_bin>0 & !missing(at_mm_both_cashin_el2_aug19_bin)

	*winsorize amounts
	foreach var of varlist ///
		at_mm_*_cashin_amt_daily 		hfps_d_dep_mm_*_today ///
		at_mm_*_cashin_amt_weekly  		hfps_d_dep_mm_*_week ///
		at_mm_*_cashin_amt_round 		hfps_d_dep_mm_*_round ///
		at_mm_*_run_bal_ms_date 		///
		ms_d_bal_mm_1  ms_d_bal_mm_2 ms_d_bal_mm_both ///
		hfps_d_dep_mm_both_el2_aug19 ///
		at_mm_both_cashin_el2_aug19 ///
		{
	gen `var'_w5=`var'
	sum `var' if `var'!=0, d
	
	replace `var'_w5=r(p95) if `var'>r(p95) & !missing(`var')
	}
	
	
*******************************
*Run MM validation regressions
*******************************

	rename at_mm_*_cashin_amt_daily_bin at_mm_*_cashin_today_bin
	rename at_mm_*_cashin_amt_daily_w5  at_mm_*_cashin_today_w5 
	rename at_mm_*_cashin_amt_weekly_bin at_mm_*_cashin_week_bin
	rename at_mm_*_cashin_amt_weekly_w5  at_mm_*_cashin_week_w5
	rename at_mm_*_cashin_amt_round_w5 at_mm_*_cashin_round_w5

	foreach round in both {
		
	local ending2 ""
	local restriction ""
	
	foreach account in both{
	local ending ""
	
	foreach X in today_bin today_w5 week_bin week_w5 round_w5 el2_aug19_bin el2_aug19_w5{
	
	local append_replace append		

	if "`X'"=="today_bin"{
	local append_replace replace
	}
	
	if "`X'"=="el2_aug19_bin" | "`X'"=="el2_aug19_w5"{
	sort id
	local restriction2 "& id!=id[_n+1]" /*single value per person*/
	}

	qui reg  at_mm_`account'_cashin_`X'	hfps_d_dep_mm_`account'_`X' if hfps_d_dep_mm_`account'_`X'!=.  `restriction' `restriction2', noconstant
	qui test hfps_d_dep_mm_`account'_`X'=1
	local p=r(p)
	qui sum at_mm_`account'_cashin_`X' 
	local mean=r(mean)
	local sd=r(sd)
	
	qui egen k=nvals(id) if at_mm_`account'_cashin_`X'!=. & hfps_d_dep_mm_`account'_`X'!=.
	qui sum k
	qui local n_id=r(mean)
	drop k
		
	outreg2 using "${path2}\tableC1_mm_self_reports_valid`ending'`ending2'.out",  `append_replace' 	nonote se   bdec(3) nocons ///  	
		addstat("control mean", `mean', "control sd", `sd', "number of clusters", `n_id' ,  "B=1 pval", `p') keep(hfps_d_dep_mm_`account'_`X')

	}
	}
	}


	
************************************************************************************************
* Table C2 - Correlations between reported and verified balances in boxes
***********************************************************************************************

*Boxes
	use "${path1}MAP2_Working_File.dta", clear

	egen lb_am_in_box_all=rowtotal(lb_am_in_box_silver lb_am_in_box_black lb_am_in_box_brown)
	replace lb_am_in_box_all=. if lb_am_in_box_silver==. & lb_am_in_box_black==. & lb_am_in_box_brown==.
	lab var lb_am_in_box_all "Verified balance in all boxes"
	
	gen lb_am_in_box_all_bin=(lb_am_in_box_all>0)
	replace lb_am_in_box_all_bin=. if missing(lb_am_in_box_all)
	
	egen lb_bx31_money_all_boxes=rowtotal(lb_bx31_money_silver_am lb_bx31_money_black_am lb_bx31_money_brown_am)
	replace lb_bx31_money_all_boxes=. if lb_bx31_money_silver_am==. & lb_bx31_money_black_am==. & lb_bx31_money_brown_am==.
	lab var lb_bx31_money_all_boxes "Reported balance in all boxes"

	gen lb_bx31_money_all_boxes_bin=(lb_bx31_money_all_boxes>0)
	replace lb_bx31_money_all_boxes_bin=. if missing(lb_bx31_money_all_boxes)
	
	gen asinh_reported_all_boxes=asinh(lb_bx31_money_all_boxes) 
	gen asinh_validated_all_boxes=asinh(lb_am_in_box_all)

***********************************************
*Run Regression - Self-deposits with Boxes
***********************************************	

	local append_replace replace
	foreach spec in all one multi {
	
	if "`spec'"=="all"{
	local ending ""
	local restriction ""
	}
	
	if "`spec'"=="one"{
	local ending "_onebox"
	local restriction " & one_box==1"
	}
	
	if "`spec'"=="multi"{
	local ending "_multibox"
	local restriction " & multi_box==1"
	}
	
	*amounts
	qui reg lb_am_in_box_all lb_bx31_money_all_boxes if id!=id[_n+1] `restriction'
	qui test lb_bx31_money_all_boxes=1
	local p=r(p)
	qui sum lb_saw_atleast1_box if id!=id[_n+1]
	local mean_1=r(mean)
	qui sum lb_saw_all_boxes if id!=id[_n+1]
	local mean_2=r(mean)
	outreg2 using "${path2}\tableC2_box_self_deposits_valid`ending'.out",  `append_replace' ///
		nonote se   bdec(3) nocons   noaster ///
		addstat("B=1 pval", `p',"Saw at least 1 box", `mean_1', "Saw all boxes", `mean_2')
	
	
	*for each box separately
	if "`spec'"=="multi" | "`spec'"=="all" {
	foreach box in silver black brown{
		qui reg lb_am_in_box_`box' lb_bx31_money_`box'_am if id!=id[_n+1] `restriction'
		qui test lb_bx31_money_`box'_am=1
		local p=r(p)
		outreg2 using "${path2}\tableC2_box_self_deposits_valid`ending'.out",  append ///
		addstat("B=1 pval", `p')  noaster nonote se   bdec(3) nocons 
		}
		}
		}
		
*******************************************************************		
*Web Appendix Figure C1. Reporting vs. verified lockbox balances*
******************************************************************8		
*#delimit
*Panel A (all boxes)
	graph twoway (scatter lb_am_in_box_all lb_bx31_money_all_boxes) (function y = x, range(0 1000) clpat(dash)) ///
	if id!=id[_n+1], graphregion(color(white)) xtitle(Reported Amount) ytitle(Verified Amount) ///
	legend( order(1 "Amount in all Boxes"  2 "45 degree line"))
	graph export "${path2}\fig_C1_panelA.png", replace
	
*Panel B (below 75th percentile)
	*zoomed in
	local zoom=100
	*#delimit
	preserve
	sum lb_am_in_box_all, d
	drop if lb_am_in_box_all>r(p75)
	sum lb_bx31_money_all_boxes, d
	drop if lb_bx31_money_all_boxes>r(p75)
	
	graph twoway (scatter lb_am_in_box_all lb_bx31_money_all_boxes) (function y = x, range(0 30) clpat(dash)) ///
	if id!=id[_n+1] , graphregion(color(white)) xtitle(Reported Amount) ytitle(Verified Amount) ///
	legend( order(1 "Amount in all Boxes" 2 "45 degree line"))
	graph export "${path2}\fig_C1_panelB.png", replace
	restore		
				
